In the Python file that controls the Jinja template I create a dictionary:
disp_list = {}
for comp in requested_workspace.components:
if comp.show_on_report_list:
disp_list[comp.label] = {}
disp_list[comp.label]['descrip'] = comp.description
In the Jinja template I want to display values from the dictionary:
<table class="table table-hover">
<tbody>
{% for listing in disp_list %}
<tr>
<td>
{{ listing }}
</td>
<td>
{{ listing['descrip'] }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ listing }} displays but {{ listing['descrip'] }} won't display. Am I accessing the 2nd value incorrectly?
The Python file controlling the Jinja template was fine. I didn't change it. The Jinja template (HTML file) was where the change was needed.
{% for item_name, values in disp_list.items() %}
<tr>
<td>
{{ item_name }}
</td>
<td>
{{ values['descrip'] }}
</td>
</tr>
{% endfor %}
Using .items() allows me to use tuple explosion and get 2 values out of the dictionary.
Related
I am trying to modify part of code in a flask application. What the flask app does so far:
It has around 20 links, when someone clicks on the link it returns .json objects (formed by python dictionaries via jsonify).
What I am trying to do:
Return the same results in a table format that can be potentially extracted to .csv
I created the following function:
def tab_out(output = {}):
return render_template('output_tab.html',output=output)
that sends the dictionary out to an html page, that contains this:
{% block content %}
<body>
<pre>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
</tr>
{% for key_1, value in output.items() %}
{% for key in value.keys() %}
<tr>
<td>
{{ key_1 | safe }}
</td>
<td>
{{ key | safe }}
</td>
<td>
{{ value[key]["Header_3"] }}
</td>
<td>
{{ value[key]["Header_4"] }}
</td>
<td>
{{ value[key]["Header_5"] }}
</td>
<td>
{{ value[key]["Header_6"] }}
</td>
</tr>
{% endfor %}
{% endfor %}
</table>
</pre>
</body>
{% endblock %}
What's the issue:
I need to make it dynamic.
Each page has different headers.
In each page the resulted .json comes from a dictionary that has sub-dictionaries and the number of keys in each dictionary can be different. So I can't specify the headers before hand. I want them dynamically created. Maybe flatten out the dictionary (up to a level 2 let's say) if there's 3 sub-dictionaries and get the results as headers and the rest as values.
I've read a couple similar questions in sa, but none of them really helped me. Any ideas?
The jinja code below will generate HTML table from any FLAT dict.
The headers are dynamic. entry_list is a list of dicts. Your code should create a flat dict.
<table style="width:100%">
<!-- table header -->
{% if entry_list %}
<tr>
{% for key in entry_list [0] %}
<th> {{ key }} </th>
{% endfor %}
</tr>
{% endif %}
<!-- table rows -->
{% for dict_item in entry_list %}
<tr>
{% for value in dict_item.values() %}
<td> {{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
I have the following code:
<table id="MyTable" class="highlight responsive-table" >
<thead>
<tr>
{% with columns as columns %}
{% for columns in columns %}
<th class="th-sm">{{ columns }}</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% for trans in transactions %}
<tr>
<td> {{ trans.transactionsequencenumber }}</td>
<td> {{ trans.posid }}</td>
<td> {{ trans.transactionnumber }}</td>
</tr>
{% endfor %}
</table>
which works for displaying static table data, but the idea is that the columns being iterated are being set via a user preferences model, so the table columns can change depending on the user, theres a possible 90+ columns to choose from, and here is where my issue lays.
pseudo code below to help illustrate my goal:
<table id="MyTable" class="highlight responsive-table" >
<thead>
<tr>
{% with columns as columns %}
{% for columns in columns %}
<th class="th-sm">{{ columns }}</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% for trans in transactions %}
<tr>
<td> {{ trans.{{columns}} }}</td>
</tr>
{% endfor %}
I've tried simple string concatenation so far, but that just prints the string to the table and not the object data,
any help and/or guidance would be greatly apprecieted.
be kind, this is my first question.
For every object that represent a line in the database attribute, you put it into a column. For example:
<tr>
<th> title </th>
<th> body </th>
</tr>
{% for post in posts %}
<tr>
<td> {{post.title}}</td>
<td> {{post.body}} </td>
</tr>
{% endfor %}
views.py
def detect_full_text(tweet):
if 'retweeted_status' in tweet.*_json*:
analysis_tweet = tweet._json['retweeted_status']['full_text']
else:
analysis_tweet = tweet.full_text
return (analysis_tweet)
public_tweet.html
{% for analyze in analysis %}
<tr>
<td>
{{ detect_full_text(analyze) }}
</td>
<td>
{{ analyze.created_at }}
</td>
<td>
{{ analyze.favorite_count }}
</td>
<td>
{{ analyze.retweet_count }}
</td>
</tr>
{% endfor %}
I want to call python function from view.py in the html page
Can anybody help me? I have a list of 60 records from django database in my html file. I want to show that list in table of 6 columns and 10 rows. In html file I used command {% for %} but the list is in one column.
My html file is:
{% for kategoria in kategorie %}
<table>
<tr>
<th> {{ kategoria.glowna|linebreaksbr }} ({{ wpisy_kat }}) </th>
</tr>
</table>
{% endfor %}
you can try using divisibleby template tag in your template like this to change row after every six entries.
remember {{ forloop.counter }} {# starting index 1 #} and {{ forloop.counter0 }} {# starting index 0 #}
{% for kategoria in kategorie %}
<table>
{% if not forloop.counter0 |divisibleby:"6" %}
<tr>
{% endif %}
<th> {{ kategoria.glowna|linebreaksbr }} ({{ wpisy_kat }}) </th>
{% if forloop.counter|divisibleby:"6" %}
</tr>
{% endif %}
</table>
{% endfor %}
<table>
{% for object in queryset %}
<tr>
<td> object.attr1 </td>
<td> object.attr2 </td>
<td> object.attr3 </td>
<td> object.attr4 </td>
<td> object.attr5 </td>
<td> object.attr6 </td>
</tr>
{% endfor %}
</table>
If you want to have a more pages with 10 items each page, have a look at django's pagination
So im trying to create an HTML table using Jinja2 from a list of dictionaries (as returned by a Flask SQL select statement).
Right now test_list has stored a list of dictionaries (with the key's being the DB columns).
Right now im using this:
<table style="width:100%">
{% for dict_item in history_list %}
{% for key, value in dict_item.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
{% endfor %}
</table>
It does work, but it's basically producing 2 columns (one being the keys and one being the columns). Im wanting to get the DB keys as columns titles in the table and then just the values placed into each column.
Is this possible? since I would want to iterate through the key's just once?
You need something like this, that provides a header row of th elements, then proceedes to the data rows (the body of the table).
<table style="width:100%">
<!-- table header -->
{% if history_list %}
<tr>
{% for key in history_list[0] %}
<th> {{ key }} </th>
{% endfor %}
</tr>
{% endif %}
<!-- table rows -->
{% for dict_item in history_list %}
<tr>
{% for value in dict_item.values() %}
<td> {{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
</table>