Skip to content

Instantly share code, notes, and snippets.

@choaimeloo
Created November 5, 2018 13:42
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 choaimeloo/4bbc6bd08e2a76fbd80833defc661731 to your computer and use it in GitHub Desktop.
Save choaimeloo/4bbc6bd08e2a76fbd80833defc661731 to your computer and use it in GitHub Desktop.
updated index function & html page
def index():
"""Show portfolio of stocks"""
stocks = db.execute("SELECT symbol, sum(shares) as sum FROM portfolio WHERE id=:id GROUP BY symbol ORDER BY symbol", id=session["user_id"])
user_id = session["user_id"]
prices = []
for stock in stocks:
symbol = stock["symbol"]
sum = int(stock["sum"])
quote = lookup(symbol)
prices.append(quote["price"])
price = quote["price"]
stock_value = (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, prices=prices, price=price, stock_value=stock_value, user_cash=user_cash, grand_total=grand_total)
{% 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>{{ prices[stock.price] }}</td>
<td>{{ prices[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