CS50金融指数。无法让数据出现在 HTML table

CS50 Finance Index. Can't get data to appear in the HTML table

我正在处理财务 pset 的索引部分。我无法让股票信息显示在 html table 中。我很难思考如何在不必使用新数据创建新字典的情况下循环浏览股票信息。有人可以帮我解决这个方向吗?有人可以给我一个 "better" 方法吗?我觉得这不是解决这个问题的 "best" 方法。希望代码显示正确,我还在学习如何使用这个网站。

@app.route("/") @login_required def index():
    """Show portfolio of stocks"""

    #set portfolio GROUPED BY symbol
    portfolio = db.execute("SELECT symbol, SUM(shares) AS share_total FROM portfolio WHERE user_id = :user_id GROUP BY symbol", user_id = session["user_id"])

    row = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
    remaining_cash = row[0]["cash"]

    total_holding_value = 0
    stock_info = {}

    for stock in portfolio:
        symbol = stock["symbol"]
        shares = stock["share_total"]
        quote = lookup(symbol)
        price = quote["price"]
        holding_value = shares * price
        total_holding_value += holding_value
        stock_info.update({"symbol":symbol, "shares":shares, "price":price, "holding_value":holding_value})

    grand_total = total_holding_value + remaining_cash

    return render_template("index.html", stock_info = stock_info, remaining_cash = remaining_cash, grand_total = grand_total)

{% extends "layout.html" %}

{% block title %}
    Portfolio
{% endblock %}

{% block main %}
    <table class="table table-bordered">
        <thead>
            <th>Symbol</th>
            <th>Shares</th>
            <th>Price</th>
            <th>Total</th>
        </thead>
        <tbody>
            {% for stock in stock_info %}
            <tr>
                <td>{{ stock.symbol }}</td>
                <td>{{ stock.shares }}</td>
                <td>{{ stock.price }}</td>
                <td>{{ stock.holding_value }}</td>
            </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td></td>
                <td>Remaining Balance:</td>
                <td>{{ remaining_cash }}</td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td>Grand Total:</td>
                <td>{{ grand_total }}</td>
            </tr>
        </tbody>
    </table>
{% endblock %}

这里有一个问题stock_info.update({"symbol":symbol, "shares":shares, "price":price, "holding_value":holding_value})。来自 python doc [empashis added].

update([other])

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

您可能想要将 列表 词典发送到 html。考虑声明 stock_info 一个列表,并使用 append 方法添加每只股票。