-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
67 lines (49 loc) · 1.66 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import redirect, render_template, session
from functools import wraps
import finnhub
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.
https://flask.palletsprojects.com/en/1.1.x/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 usd(value):
"""Format value as USD."""
return f"${value:,.2f}"
def autocomplete():
"""Look up complete list of stock symbols."""
# Use API key for Finnhub
finnhub_client = finnhub.Client(api_key="ce2mje2ad3i1c7jetfr0ce2mje2ad3i1c7jetfrg")
# Initiate list of symbols and empty dictionary of names
stock_symbols = []
# Get stock data from US exchanges and append symbols to symbol list
for stock in finnhub_client.stock_symbols("US"):
stock_symbols.append(stock["symbol"])
# Sort stock names alphabetically by description
stock_symbols = sorted(stock_symbols)
return stock_symbols