python - How to render lists of column values into a table using a template (such as Jinja2) -
i trying following (doesn't work, shown convey intended behavior):
<table> <tr> <th>column 1</th> <th>column 2</th> <th>column 3</th> </tr> {% col1_val in col1_values %} <tr> <td>{{ col1_val }}</td> <td>{{ col2_values[col1_values.index(col1_val)] }}</td> <td>{{ col3_values[col1_values.index(col1_val)] }}</td> </tr> {% endfor %} </table>
where table desired is:
column 1 column 2 column 3 col1_values[0] col2_values[0] col3_values[0] col1_values[1] col2_values[1] col3_values[1] . . .
where col1_values unique each other.
how should jinja2 template rewritten achieve desired table output? possible without having transpose dimensions of col1_values, col2_values , col3_values? if not, pythonic way of doing transpose?
why not use nested list instead? loop on structure like:
table_values = [[col1_value_0, col2_value_0, col3_value_0], [col1_value_1, col2_value_1, col3_value_1], ...]
the zip() function can combine 3 colx_values lists if need be:
table_rows = zip(col1_values, col2_values, col3_values)
now have per-row lists loop over:
{% col1_val, col2_val, col3_val in table_rows %} <tr> <td>{{ col1_val }}</td> <td>{{ col2_val }}</td> <td>{{ col3_val }}</td> </tr> {% endfor %}
Comments
Post a Comment