Skip to content

Instantly share code, notes, and snippets.

/application.py Secret

Created April 2, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/837a3f19ac680ccf0ee06cfff647fb86 to your computer and use it in GitHub Desktop.
Save anonymous/837a3f19ac680ccf0ee06cfff647fb86 to your computer and use it in GitHub Desktop.
application.py - shared from CS50 IDE
@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