Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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)
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">
<th>Total Value</th>
{% for stock in stocks %}
<td>{{ stock.symbol }}</td>
<td>{{ stock.sum }}</td>
<td>{{ prices[stock.price] }}</td>
<td>{{ prices[stock.stock_value] }}</td>
{% endfor %}
<h5>Your total cash: {{ user_cash }}<br>
Your grand total: {{ grand_total }}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment