0

I have been trying to populate these output in flask template table view.But when it renders it displays the last list value in table (overwriting first list ). Can anyone please help me on how to create a new column when second list comes in to the picture?

Python code:

for i in mac:
    #sqlite query
    usage_list = cur.fetchall()

output of usage_list:

[5.0, 5.0, 5.0, 5.0, 5.0]
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0]

HTML code

{% for item in usagelist %}
  <tr>
     <td> {{item}} </td>     

  </tr>
 {% endfor %}

Desired output: ex:

col 1  col 2

10.0   0.0
10.0   5.0
10.0   5.0
10.0   5.0
10.0   0.0
10.0   5.0 
3
  • Each loop iteration generates a row with a single column. Are you sure this is what you want? To create a table, you probably need two nested loops, one for rows and one for columns. Commented Aug 11, 2017 at 14:30
  • @FlorianWinter Winter you are correct.I have tried with two nested loops.I can split those values,but can you suggest me how to pass those list for column and row separately? Commented Aug 12, 2017 at 4:00
  • You are talking about two lists, but in your code, there is only one list, called usage_list. Also, under "output of usage_list", you show two lists. Do you mean there is another variable, say list2, which is another list and which should be used to populate the second column? Commented Aug 15, 2017 at 15:55

1 Answer 1

1

If you cannot (easily) build a loop construct in the template engine that fits your needs, you can always transform the data before passing it to the template engine.

For example, you can transform two lists into a list of lists in Python:

table_data = [[list1[i], list2[i]] for i in range(0, len(list1))]

(You can probably also use itertools.izip here...)

then use nested loops to render the transformed data in jinja:

{% for row in tabledata %}
<tr>
  {% for item in row %}
  <td> {{item}} </td>     
  {% endfor %}
</tr>
{% endfor %}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks i will check with this and get back to you and sorry for the late reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.