update
This commit is contained in:
parent
5f263f5bb3
commit
06de56ddd9
1
app.py
1
app.py
|
|
@ -1,7 +1,6 @@
|
||||||
from flask import Flask, render_template, request, redirect
|
from flask import Flask, render_template, request, redirect
|
||||||
from sympy import *
|
from sympy import *
|
||||||
|
|
||||||
from helpers import apology, login_required, lookup, usd
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
|
|
||||||
23
helpers.py
23
helpers.py
|
|
@ -1,5 +1,4 @@
|
||||||
import requests
|
import requests
|
||||||
import urllib3.parse
|
|
||||||
|
|
||||||
from flask import redirect, render_template, request, session
|
from flask import redirect, render_template, request, session
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
@ -34,28 +33,6 @@ def login_required(f):
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
||||||
|
|
||||||
def lookup(symbol):
|
|
||||||
"""Look up quote for symbol."""
|
|
||||||
|
|
||||||
# Contact API
|
|
||||||
try:
|
|
||||||
response = requests.get(f"https://api.iextrading.com/1.0/stock/{urllib.parse.quote_plus(symbol)}/quote")
|
|
||||||
response.raise_for_status()
|
|
||||||
except requests.RequestException:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Parse response
|
|
||||||
try:
|
|
||||||
quote = response.json()
|
|
||||||
return {
|
|
||||||
"name": quote["companyName"],
|
|
||||||
"price": float(quote["latestPrice"]),
|
|
||||||
"symbol": quote["symbol"]
|
|
||||||
}
|
|
||||||
except (KeyError, TypeError, ValueError):
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def usd(value):
|
def usd(value):
|
||||||
"""Format value as USD."""
|
"""Format value as USD."""
|
||||||
return f"${value:,.2f}"
|
return f"${value:,.2f}"
|
||||||
|
|
|
||||||
61
helpers0.py
61
helpers0.py
|
|
@ -1,61 +0,0 @@
|
||||||
import requests
|
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
from flask import redirect, render_template, request, session
|
|
||||||
from functools import wraps
|
|
||||||
|
|
||||||
|
|
||||||
def apology(message, code=400):
|
|
||||||
"""Render message as an apology to user."""
|
|
||||||
def escape(s):
|
|
||||||
"""
|
|
||||||
Escape special characters.
|
|
||||||
|
|
||||||
https://github.com/jacebrowning/memegen#special-characters
|
|
||||||
"""
|
|
||||||
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"),
|
|
||||||
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]:
|
|
||||||
s = s.replace(old, new)
|
|
||||||
return s
|
|
||||||
return render_template("apology.html", top=code, bottom=escape(message)), code
|
|
||||||
|
|
||||||
|
|
||||||
def login_required(f):
|
|
||||||
"""
|
|
||||||
Decorate routes to require login.
|
|
||||||
|
|
||||||
http://flask.pocoo.org/docs/1.0/patterns/viewdecorators/
|
|
||||||
"""
|
|
||||||
@wraps(f)
|
|
||||||
def decorated_function(*args, **kwargs):
|
|
||||||
if session.get("user_id") is None:
|
|
||||||
return redirect("/login")
|
|
||||||
return f(*args, **kwargs)
|
|
||||||
return decorated_function
|
|
||||||
|
|
||||||
|
|
||||||
def lookup(symbol):
|
|
||||||
"""Look up quote for symbol."""
|
|
||||||
|
|
||||||
# Contact API
|
|
||||||
try:
|
|
||||||
response = requests.get(f"https://api.iextrading.com/1.0/stock/{urllib.parse.quote_plus(symbol)}/quote")
|
|
||||||
response.raise_for_status()
|
|
||||||
except requests.RequestException:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Parse response
|
|
||||||
try:
|
|
||||||
quote = response.json()
|
|
||||||
return {
|
|
||||||
"name": quote["companyName"],
|
|
||||||
"price": float(quote["latestPrice"]),
|
|
||||||
"symbol": quote["symbol"]
|
|
||||||
}
|
|
||||||
except (KeyError, TypeError, ValueError):
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def usd(value):
|
|
||||||
"""Format value as USD."""
|
|
||||||
return f"${value:,.2f}"
|
|
||||||
|
|
@ -1,6 +1,15 @@
|
||||||
flask
|
certifi==2019.11.28
|
||||||
sympy
|
chardet==3.0.4
|
||||||
numpy
|
Click==7.0
|
||||||
urllib3
|
Flask==1.1.1
|
||||||
functools
|
gunicorn==20.0.4
|
||||||
requests
|
idna==2.8
|
||||||
|
itsdangerous==1.1.0
|
||||||
|
Jinja2==2.10.3
|
||||||
|
MarkupSafe==1.1.1
|
||||||
|
mpmath==1.1.0
|
||||||
|
numpy==1.17.4
|
||||||
|
requests==2.22.0
|
||||||
|
sympy==1.4
|
||||||
|
urllib3==1.25.7
|
||||||
|
Werkzeug==0.16.0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue