Skip to content

Instantly share code, notes, and snippets.

/ Secret

Created Apr 2, 2017
What would you like to do? - shared from CS50 IDE
def index():
# if user reached route via POST (as submitting a form via POST)
if request.method == "GET":
stocks = []
# query portfolio for stock holdings
holdings = db.execute("SELECT stock, shares FROM portfolio WHERE id = :id", id=session['user_id'])
# if user has stocks in their portfolio
if holdings != []:
for dict in holdings:
symbol = str(dict['stock'])
num_shares = dict['shares']
stock_info= lookup(symbol)
name = (stock_info['name'])
price = (stock_info['price'])
value = (num_shares * price)
stocks = [symbol, name, num_shares, price, value]
return render_template("index.html", holdings=holdings, stocks=stocks, symbol=symbol, num_shares=num_shares, name=name, price=price, value=value)
# return empty index table if user has no stocks in portfolio
return render_template("empty_index.html")
# else if user reached route via GET (as by clicking a link or via redirect)
return render_template("index.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment