Here is my index route for application.py: https://gist.github.com/837a3f19ac680ccf0ee06cfff647fb86

Here is the outline of my index.html file: https://gist.github.com/acf68454e8cd75ae566bec06781d93a5

I have been working on index everyday for about 2 weeks and I am totally stuck. I have tried multiple different implementations with looping on the backend, loops in my html, loops in both places. I have read the flask documentation, read tutorials on html, python methods, etc. and have gotten nowhere. I can pass one set of values successfully to my index.html page, what I want to do is to loop through holdings, obtain each corresponding value in my code, and then pass them to my index.html table.

2 Answers 2


What is going to be stored into your holdings value is "A list of dictionaries", where each list item will be one stock.

i.e each list item will correspond to each row of your query: "SELECT stock, shares FROM portfolio WHERE id = :id"

Now iterating over a simple list of fruits ( eg: fruits = ['apples', 'oranges', 'pears', 'apricots'] ) can be done simply as:

for fruit in fruits:

    print "A fruit of type: %s" % fruit

Similarly you can iterate over each holding value as:

for dict in holdings:
    ...TO DO...

Now you have stored the required values in different variables and want to do the same looping in html to print table data. This can be done in the same way in jinja:

syntax for printing a variable name:

{{ variable_name }}

syntax for 'for loop'

{% for item in seq -%}
    {{ item }}
{%- endfor %}

Hence you can do the required task somewhat as:

    {% for dict in holdings %}
            <td>{{ symbol }}</td>
            <td>{{ num_shares }}</td>
    {% endfor %}

ref: http://jinja.pocoo.org/docs/2.9/templates/

Hope this helps. :)

  • This still doesn't loop through the values in stocks and shares. Commented Apr 3, 2017 at 0:18
  • It takes the first set of values from dict and prints it over and over equal to the number of stocks in the list. Commented Apr 3, 2017 at 0:20
  • It is doing so because of what you are storing in your variables (symbol, num_shares, etc). You are basically storing only a single value in those variables. What you can do is not use these variables to store value and directly access them in jinja as {{ dict['stock'] }}, {{ dict['shares'] }}, etc.
    – Hydes
    Commented Apr 3, 2017 at 4:50
  • Also "stocks = [symbol, name, num_shares, price, value]" will be storing a single value, another way can be to convert this into a list of dictionaries and pass only holdings and stocks, no need for extra arguments.
    – Hydes
    Commented Apr 3, 2017 at 4:57
  • I have erased all of my code and started over in order to try and get just the stock symbols and corresponding shares into my table. I can get the values of 'stock' and 'shares' to show up in my html table, they all print on the same line though, there are no separate rows of information. Commented Apr 4, 2017 at 1:22

I rewrote my python code as follows:

Query SQL db return stock and shares columns for logged in user, store in variable, rows.

Create an empty list.

If my SQL query is not empty:

For row in rows:

Use lookup function for each stock.

Create dict and store within it, the values for each row in the table(symbol, name, shares, current price, valuation). These are stored in the dict in "key": value pairs.

Use list.append() method to update the stocks list for each pass through row.

My index.html file is where I was having the biggest problem. I rewrote as follows:

Created table headings.

Create table body.

Loop through stocks.

(Here was my mistake, I placed the loop inside my < tr > tags)

< tr >

< td > {{stock.key}} < /td >


< /tr >

{ % endfor % }

  • I'm sorry you didn't bother to read my comments where I said I was erasing my code and starting over and then I posted what I did that fixed my problem. Commented Apr 6, 2017 at 3:59

You must log in to answer this question.

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