how to convert a list into html column in python
I have been trying to work with py开发者_JS百科thon and using jinja template for my HTML rendering. The problem I have is that , I have two seperate lists.
Column_List [col_name1,col_name2,....]
Data_List
[val1_col_name1,val2_col_name2,...]
[val3_col_name1,val4_col_name2,...]
[val1_col_name1,val2_col_name2,...]
So , any pointers on how do I convert this to an HTML table with column name and with an assosiated row data ?
col1 col2 col3 ...
dat1 dat2 dat3
dat4 dat5 dat6
Supposing that your variables are passed to jinja with the using the same names:
col_names = [col_name1, col_name2, ...]
data = [
[val1_col_name1, val2_col_name2, ...]
[val3_col_name1, val4_col_name2, ...]
[val1_col_name1, val2_col_name2, ...]
]
Then, the following jinja snippet is not that complicated:
<table>
<tr>
{% for col_name in col_names %}
<th>{{ col_name }}</th>
{% endfor %}
</tr>
{% for row in data %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
精彩评论