I am stuck on index() in finance. I have a working SQL function which compiles all the transactions made by the user and sums the shares of all the repeating stocks - ie creating a list of owned stocks and how many shares there are. I have checked that the SQL function returns correct data. I have a for loop which creates a list with all the needed data points for the table in index (current price, total holding price, stock name, stock symbol...) This list is passed into the html table but the html only displays the headings of the table.

Here's app.py:

def index():
    """Show portfolio of stocks"""
    id = session["user_id"]
    stocks = db.execute("SELECT stock_name, SUM(shares), stock_symbol FROM transactions GROUP BY stock_name HAVING user_id = ?", id)
    portfolio = []
    for stock in stocks:
        stock_name = stock["stock_name"]
        stock_symbol = stock["stock_symbol"]
        shares = stock["SUM(shares)"]
        quote = lookup(stock_symbol)
        current_price = quote["price"]
        shares_value = current_price*shares
        portfolio.append([stock_name, stock_symbol, shares, current_price, shares_value])
    return render_template("index.html", portfolio = portfolio)

Here's index.html:

{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <h2>Your portfolio</h2>
                <th>Stock Name</th>
                <th>Stock Symbol</th>
                <th>Total Shares</th>
                <th>Current Stock Price</th>
                <th>Total Shares Value</th>

            {% for stock in portfolio %}
                    <td>{{ stock.stock_name }}</td>
                    <td>{{ stock.stock_symbol }}</td>
                    <td>{{ stock.shares }}</td>
                    <td>{{ stock.current_price }}</td>
                    <td>{{ stock.shares_value }}</td>
            {% endfor %}
{% endblock %}

And finally here's the screenshot of the output: The table

Please help me

1 Answer 1


The html is using dictionary syntax for portfolios (ie stock.stock_name). But it is a list of lists, so elements would be addresssed by index (eg stock[0]).

You might want to view source in the browser (usually with a right-click context menu) and look at the table. I would expect n blank rows with five td elements (where n is the number of elements in portfolio).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .