I finally made some forward progress by myself, and I have all the variable rows filled in, buy the stock and symbol show the same thing, when I would like one to say "AAPL" and the other to say "Apple" and I'm not sure what to change to get it to do this. Also, if there is anything else I need to add to index to make it better or correct, please tell me, I still have to implement sell. Code:
def index():
"""Show portfolio of stocks"""
# Once a user makes a purchase they will be redirected to their index page
# Edit that to reflect the user's portfolio
# Get symbol, shares, cash from databases
purchases = db.execute("SELECT stock, SUM(shares) as sumshares FROM transactions WHERE user_id = :user_id GROUP BY stock ORDER BY stock ASC", user_id = session["user_id"])
usercash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
usercash = usercash[0]["cash"]
# Declare variable for running total of stock value
totalvalstock = 0
# PDHTEFW7T4KOTDHI
# Purchases query will return dicts, loop to iterate over list of dicts.
for purchase in purchases:
stock = purchase["stock"]
shares = purchase['sumshares']
purchase['symbol'] = purchase['stock']
currentstock = lookup(purchase['stock'])
purchase['price'] = currentstock['price']
purchase['singlestocktotal'] = purchase['sumshares'] * purchase['price']
purchase["totalvalstock"] = round(purchase['singlestocktotal'],2)
totalvalstock += purchase['totalvalstock']
# calculate total of all stock holdings at their current price and whatever cash the user has left.
grandtotalstocks = round(totalvalstock,2)
overalltotal = round(grandtotalstocks + usercash,2)
# call render_template and pass variables to html template
return render_template("index.html", purchases=purchases, usercash=usercash, grandtotalstocks=grandtotalstocks,overalltotal=overalltotal,)
{% extends "layout.html" %}
{% block title %} Index {% endblock %}
{% block main %}
<table width = 100%>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>Value</th>
</tr>
{% for purchase in purchases %}
<tr>
<td>{{purchase.symbol}}</td>
<td>{{purchase.stock}}</td>
<td>{{purchase.sumshares}}</td>
<td>{{purchase.price}}</td>
<td>{{purchase.totalvalstock}}</td>
</tr>
{% endfor %}
<tr>
<td>Total Value of Stocks</td>
<td> </td>
<td> </td>
<td> </td>
<td>{{grandtotalstocks}}</td>
</tr>
<tr>
<td>Cash</td>
<td> </td>
<td> </td>
<td> </td>
<td>{{usercash}}</td>
</tr>
<tr>
<td>Total Assets</td>
<td> </td>
<td> </td>
<td> </td>
<td>{{overalltotal}}</td>
</tr>
</table>
{% endblock %}
Also here's a screenshot for index