I am trying to do the index part of this pset but the problem is since I want the price to be the current price, not price bought, I need to use the lookup function. But how do I display the price? The problem with my code is that it doesn't display the price and the total per stock. Also, how will I present the price using the usd() function since I can't use usd in the html. Thank you in advance. My code is this:
def index():
"""Show portfolio of stocks"""
userid = session["user_id"]
index = db.execute("SELECT symbol, SUM(shares) AS shares, name FROM transactions WHERE user_id = :user_id GROUP BY symbol", user_id=userid)
# this will get the cash of the user
cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=userid)
money = cash[0]["cash"]
total = money
for row in index:
quote = lookup(row["symbol"])
shares = int(row["shares"])
price = float(quote["price"])
cost = price * shares
total = total + cost
return render_template("index.html", index=index, money=usd(money), total=usd(total))
and my HTML code is
<table>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>Total</th>
</tr>
{% for row in index %}
<tr>
<td>{{row["symbol"]}}</td>
<td>{{row["name"]}}</td>
<td>{{row["shares"]}}</td>
<td>{{row["price"]}}</td>
<td>{{row["cost"]}}</td>
</tr>
{% endfor %}
<tr>
<td><b>CASH</b></td>
<td colspan = "3"></td>
<td>{{money}}</td>
</tr>
<tr>
<td colspan = "4"></td>
<td>{{total}}</td>
</tr>
</table>