Despite reading documentation I cannot figure out why my loop is not functioning properly and is only dynamic for stock symbol? Am I meant to be using multiple nested loops passing all prices, holding values, etc as dictionaries?
Please look at the below and any subtle guidance would be appreciated.
Python:
@app.route("/")
@login_required
def index():
stocks = db.execute("SELECT DISTINCT stock FROM portfolio WHERE id = :id", id = (session["user_id"]))
wealth = db.execute("SELECT SUM(number*price_current) FROM portfolio WHERE id = :id", id = (session["user_id"]))
wealth = wealth[0]
wealth = wealth.get("wealth")
cash = db.execute("SELECT cash FROM users WHERE id = :id", id = (session["user_id"]))
cash = cash[0]
cash = cash.get("cash")
for stock in stocks:
symbol = stock.get("stock")
quote = lookup(symbol)
price_current = int(quote["price"])
db.execute("UPDATE portfolio SET price_current = price_current WHERE stock = :stock", stock = symbol)
share_count=db.execute("SELECT sum(number) FROM portfolio WHERE stock = :stock", stock = symbol)
share_count = share_count[0]
share_count = share_count.get("sum(number)")
holding = price_current*share_count
return render_template("index.html", stocks = stocks, stock = stock, symbol = symbol, share_count = share_count, price_current = price_current,
holding = holding, cash = cash, wealth = wealth)
And then the plainest flask code (index.html) (I will only make it graphically more appealing after getting function right):
{% extends "layout.html" %}
{% block title %}
Portfolio overview
{% endblock %}
{% block main %}
{% for stock in stocks%}
<p>{{ stock }}</p>
<p>{{ symbol }}</p>
<p>{{ share_count }}</p>
<p>{{ price_current }}</p>
<p>{{ holding }}</p>
{%endfor%}
<p>{{ cash }}</p>
<p>{{ wealth }}</p>
{% endblock %}