Last active
November 5, 2018 05:08
-
-
Save choaimeloo/e21a4eb13790becfa3a43a2ba6a91220 to your computer and use it in GitHub Desktop.
index function & html for pset7
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
def index(): | |
"""Show portfolio of stocks""" | |
stocks = db.execute("SELECT symbol, sum(shares) as sum FROM portfolio WHERE id=:id GROUP BY symbol", id=session["user_id"]) | |
for stock in stocks: | |
symbol = stock["symbol"] | |
sum = int(stock["sum"]) | |
# Check current share price & calculate total value of holdings | |
quote = lookup(stock["symbol"]) | |
price = float(quote["price"]) | |
stock_value = float(sum * price) | |
# Calculate current cash balance & grand total | |
cash = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"]) | |
user_cash = float(cash[0]["cash"]) | |
grand_total = stock_value + user_cash | |
return render_template("index.html", stocks=stocks, sum=sum, price=usd(price), stock_value=usd(stock_value), user_cash=usd(user_cash), grand_total=usd(grand_total)) | |
return render_template("index.html") | |
{% extends "layout.html" %} | |
{% block title %} | |
Your Portfolio | |
{% endblock %} | |
{% block main %} | |
<table class="table table-striped"> | |
<thead> | |
<tr> | |
<th>Symbol</th> | |
<th>Shares</th> | |
<th>Price</th> | |
<th>Total Value</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for stock in stocks %} | |
<tr> | |
<td>{{ stock.symbol }}</td> | |
<td>{{ stock.sum }}</td> | |
<td>{{ stock.price }}</td> | |
<td>{{ stock.stock_value }}</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table><br> | |
<h5>Your total cash: {{ user_cash }}<br> | |
Your grand total: {{ grand_total }} | |
</h5> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment