I can understand the for loops great in c, but seem to be hitting a wall here.
Some generic conceptual questions before i get into it:
what sequence does the program run, does it read HTML first or the Python scripts first?
what is the difference between the for loop in HTML and for loop in python?
Main/In depth question:
I can't seem to to get my "Position" dict to loop. My sharesOwned
are looping great, but not the dict(position
) i created. Can someone tell me why please?
Index in python/flask:
@app.route("/")
@login_required
def index():
#get and consolidate user data in database
sharesOwned = db.execute("SELECT symbol, price, name, SUM(shares) AS quanties FROM shares WHERE id=:id GROUP by symbol, symbol ORDER by symbol", id=session["user_id"])
#loop through "position" dict
for share in sharesOwned:
sharesNow=lookup(share['symbol'])
position={
'name':sharesNow["name"],
'pricey': sharesNow['price'],
'symbol':sharesNow["symbol"]
}
rows=db.execute("SELECT * FROM users where id = :id", id = session["user_id"])
return render_template("index.html", sharesOwned=sharesOwned, position=position, name=rows[0]["username"])
index.html:
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<h2>RESULTS:</h2>
<ul>
<li><h2> {{ name }}</h2></li>
</ul>
<table class="table table-striped">
<thead>
<th>Share Symbol</th>
<th>Shares Name</th>
<th>Number of Shares</th>
<th>Current price</th>
<th>Total value</th>
</thead>
{% for share in sharesOwned %}
<tr>
<td>{{ share.symbol }}</td>
<td>{{ position.name }}</td>
<td>{{ share.quanties }}</td>
<td>{{ position.pricey }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
Result:
