CS50 Finance - 如何在索引中正确显示循环值
CS50 Finance - How to correctly display looped values in Index
这里的新程序员正在学习 CS50x。处理财务问题集,其中我正在创建一个网站,用户可以在其中买卖股票并查看他们的交易历史记录。使用 flask、Jinja 和 Python.
对于交易历史,我从两个地方抓取数据:1) 我设置的数据库,2) 通过使用 API.
的查找函数
我的问题是来自 API 的值是正确的,但最后一行的值覆盖了所有其他行的值。我怀疑这是一个循环问题,但无法找出解决方案。我已经尝试阅读 Jinja 文档以及将 'portfolio' 更改为字典列表,将我的 Jinja 代码从 'portfolio.price' 更改为 'price' 和 'row.price' 等。我看过了有关其他堆栈溢出查询的答案。
这是我的页面当前的样子:Stocks Portfolio index。这是我的 Python 代码:
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# Access portfolio info from database
rows = db.execute("SELECT name, symbol, SUM(shares) AS shares FROM purchases WHERE users_id = ? GROUP BY name, symbol", session["user_id"])
# Access cash balance for user
user_info = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])
cash = user_info[0]["cash"]
if not rows or not cash:
return render_template("empty_portfolio.html")
else:
# Declare a dictionary to hold database info
portfolio = {}
sum_total_value = 0
# Iterate over database info and add it to portfolio
for row in rows:
portfolio.update(row)
name = row["name"]
symbol = row["symbol"]
shares = row["shares"]
stock = lookup(symbol)
price = stock["price"]
total_value = shares*price
portfolio["price"] = price
portfolio["total_value"] = total_value
print("PORTFOLIO VALUES INSIDE LOOP:", portfolio)
print("PORTFOLIO VALUES OUTSIDE LOOP:", portfolio)
sum_total_value += total_value
grand_total = cash + sum_total_value
# deliver the iterations and dictionary info to the html page
return render_template("index.html", rows=rows, portfolio=portfolio, price=price total_value=total_value, cash=cash, grand_total=grand_total)
和html:
<table>
<thead>
<tr>
<th>Stock</th>
<th>Symbol</th>
<th>Shares</th>
<th>Price</th>
<th>Total Value</th>
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
<td>{{ row.name }}</td>
<td>{{ row.symbol.upper() }}</td>
<td>{{ row.shares }}</td>
<td>{{ portfolio.price | usd }}</td>
<td>{{ portfolio.total_value | usd}}</td>
</tr>
{% endfor %}
<tr>
<td>Cash Balance: {{ cash | usd }}</td>
</tr>
<tr>
<td>GRAND TOTAL: {{ grand_total | usd }}</td>
</tr>
</tbody>
</table>
{% endblock %}
我也试过调试。这是我的终端。 请注意,我知道共享 API 密钥是个坏主意。这是一个学生项目——永远不会脱离开发领域。我还没有学会如何在我的终端中隐藏这个密钥。
调试:开始新的 HTTPS 连接 (1):cloud.iexapis.com:443
调试:https://cloud.iexapis.com:443 "GET /stable/stock/aapl/quote?token=pk_05c43fc7cb604812a604a863e7a0153c HTTP/1.1" 200 None
循环内的投资组合价值:{'name':'Apple Inc','symbol':'aapl','shares':4,'price':131.81,'total_value':527.24}
调试:开始新的 HTTPS 连接 (1):cloud.iexapis.com:443
调试:https://cloud.iexapis.com:443 "GET /stable/stock/nflx/quote?token=pk_05c43fc7cb604812a604a863e7a0153c HTTP/1.1" 200 None
循环内的投资组合价值:{'name':'NetFlix Inc','symbol':'nflx','shares':10,'price':553.885,'total_value':5538.85}
调试:开始新的 HTTPS 连接 (1):cloud.iexapis.com:443
调试:https://cloud.iexapis.com:443 "GET /stable/stock/orcl/quote?token=pk_05c43fc7cb604812a604a863e7a0153c HTTP/1.1" 200 None
循环内的投资组合价值:{'name':'Oracle Corp.','symbol':'orcl','shares':2,'price':76.005,'total_value':152.01}
循环外的投资组合价值:{'name':'Oracle Corp.','symbol':'orcl','shares':2,'price':76.005,'total_value':152.01}
信息:192.168.40.90 - - [12/Apr/2021 17:42:48] "GET / HTTP/1.0" 200 -
信息:192.168.40.90 - - [12/Apr/2021 17:42:48] "GET /static/styles.css HTTP/1.0" 200 -
这本身并不是一个真正的循环问题。 portfolio
中正好有一个键 price
,所以它自然会得到它最后设置的值。同上 total_price
.
要考虑的一个选项是一起消除 portfolio
。从数据库填充 rows
后,对其进行迭代并将 price
和 total_price
键添加到每一行。则数据“自成体系”,一row
一<tr>
.
应有尽有
这里的新程序员正在学习 CS50x。处理财务问题集,其中我正在创建一个网站,用户可以在其中买卖股票并查看他们的交易历史记录。使用 flask、Jinja 和 Python.
对于交易历史,我从两个地方抓取数据:1) 我设置的数据库,2) 通过使用 API.
的查找函数我的问题是来自 API 的值是正确的,但最后一行的值覆盖了所有其他行的值。我怀疑这是一个循环问题,但无法找出解决方案。我已经尝试阅读 Jinja 文档以及将 'portfolio' 更改为字典列表,将我的 Jinja 代码从 'portfolio.price' 更改为 'price' 和 'row.price' 等。我看过了有关其他堆栈溢出查询的答案。
这是我的页面当前的样子:Stocks Portfolio index。这是我的 Python 代码:
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# Access portfolio info from database
rows = db.execute("SELECT name, symbol, SUM(shares) AS shares FROM purchases WHERE users_id = ? GROUP BY name, symbol", session["user_id"])
# Access cash balance for user
user_info = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])
cash = user_info[0]["cash"]
if not rows or not cash:
return render_template("empty_portfolio.html")
else:
# Declare a dictionary to hold database info
portfolio = {}
sum_total_value = 0
# Iterate over database info and add it to portfolio
for row in rows:
portfolio.update(row)
name = row["name"]
symbol = row["symbol"]
shares = row["shares"]
stock = lookup(symbol)
price = stock["price"]
total_value = shares*price
portfolio["price"] = price
portfolio["total_value"] = total_value
print("PORTFOLIO VALUES INSIDE LOOP:", portfolio)
print("PORTFOLIO VALUES OUTSIDE LOOP:", portfolio)
sum_total_value += total_value
grand_total = cash + sum_total_value
# deliver the iterations and dictionary info to the html page
return render_template("index.html", rows=rows, portfolio=portfolio, price=price total_value=total_value, cash=cash, grand_total=grand_total)
和html:
<table>
<thead>
<tr>
<th>Stock</th>
<th>Symbol</th>
<th>Shares</th>
<th>Price</th>
<th>Total Value</th>
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
<td>{{ row.name }}</td>
<td>{{ row.symbol.upper() }}</td>
<td>{{ row.shares }}</td>
<td>{{ portfolio.price | usd }}</td>
<td>{{ portfolio.total_value | usd}}</td>
</tr>
{% endfor %}
<tr>
<td>Cash Balance: {{ cash | usd }}</td>
</tr>
<tr>
<td>GRAND TOTAL: {{ grand_total | usd }}</td>
</tr>
</tbody>
</table>
{% endblock %}
我也试过调试。这是我的终端。 请注意,我知道共享 API 密钥是个坏主意。这是一个学生项目——永远不会脱离开发领域。我还没有学会如何在我的终端中隐藏这个密钥。
调试:开始新的 HTTPS 连接 (1):cloud.iexapis.com:443 调试:https://cloud.iexapis.com:443 "GET /stable/stock/aapl/quote?token=pk_05c43fc7cb604812a604a863e7a0153c HTTP/1.1" 200 None 循环内的投资组合价值:{'name':'Apple Inc','symbol':'aapl','shares':4,'price':131.81,'total_value':527.24} 调试:开始新的 HTTPS 连接 (1):cloud.iexapis.com:443 调试:https://cloud.iexapis.com:443 "GET /stable/stock/nflx/quote?token=pk_05c43fc7cb604812a604a863e7a0153c HTTP/1.1" 200 None 循环内的投资组合价值:{'name':'NetFlix Inc','symbol':'nflx','shares':10,'price':553.885,'total_value':5538.85} 调试:开始新的 HTTPS 连接 (1):cloud.iexapis.com:443 调试:https://cloud.iexapis.com:443 "GET /stable/stock/orcl/quote?token=pk_05c43fc7cb604812a604a863e7a0153c HTTP/1.1" 200 None 循环内的投资组合价值:{'name':'Oracle Corp.','symbol':'orcl','shares':2,'price':76.005,'total_value':152.01} 循环外的投资组合价值:{'name':'Oracle Corp.','symbol':'orcl','shares':2,'price':76.005,'total_value':152.01} 信息:192.168.40.90 - - [12/Apr/2021 17:42:48] "GET / HTTP/1.0" 200 - 信息:192.168.40.90 - - [12/Apr/2021 17:42:48] "GET /static/styles.css HTTP/1.0" 200 -
这本身并不是一个真正的循环问题。 portfolio
中正好有一个键 price
,所以它自然会得到它最后设置的值。同上 total_price
.
要考虑的一个选项是一起消除 portfolio
。从数据库填充 rows
后,对其进行迭代并将 price
和 total_price
键添加到每一行。则数据“自成体系”,一row
一<tr>
.