-
-
Save anonymous/837a3f19ac680ccf0ee06cfff647fb86 to your computer and use it in GitHub Desktop.
application.py - shared from CS50 IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route("/") | |
@login_required | |
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 | |
else: | |
return render_template("empty_index.html") | |
# else if user reached route via GET (as by clicking a link or via redirect) | |
else: | |
return render_template("index.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment