I have a list of dictionaries that needs to be comma-separated except for last item.
For eg list of dictionaries should be something like this:
education_details: [{degree: "B.S.", type: "Full-type", location:"aus",grade:"A"}, {degree: "MCA", type: "Full-type", location:"aus",grade:"B"}]
I was rendering the above details(education_details) in my jinja template like below:
{% for data in profile.get('education_details', []) %}
{% if data.get('degree', '') %}
{% if not loop.last %}
<span>{{data.get('degree', '')}},</span>
{% else %}
<span>{{data.get('degree', '')}}</span>
{% endif %}
{% endif %}
{% if data.get('type', '') %}
{% if not loop.last %}
<span>{{data.get('type', '')}},</span>
{% else %}
<span>{{data.get('type', '')}}</span>
{% endif %}
{% endif %}
{% if data.get('location', '') %}
{% if not loop.last %}
<span>{{data.get('location', '')}},</span>
{% else %}
<span>{{data.get('location', '')}}</span>
{% endif %}
{% endif %}
{% endfor %}
I tried by using last, it's not working
Expected o/p:
Commas to appear after each field except for the last entered item
degree
degree, type, location
degree, location
Based on your sample data and expected o/p, I assume you want the output like this:
B.S., Full-type, aus
MCA, Full-type, aus
If this is the case, what you want is to print each list item row by row and to print each dictionary item's value one by one separated by , except last value.
If so, there is no need to use loop.last at all, instead, you just need to check the sequence of each dictionary key. Below is the code snippet (flask + jinja2) for that:
template = """
{% for data in profile.get('education_details', []) %}
{% if data.get('degree', '') %}
<span>{{data.get('degree', '')}},</span>
{% endif %}
{% if data.get('type', '') %}
<span>{{data.get('type', '')}},</span>
{% endif %}
{% if data.get('location', '') %}
<span>{{data.get('location', '')}}</span>
{% endif %}
<br/>
{% endfor %}
"""
education_details = [{"degree": "B.S.", "type": "Full-type", "location":"aus","grade":"A"},
{"degree": "MCA", "type": "Full-type", "location":"aus","grade":"B"}]
return render_template_string(template, profile={"education_details": education_details})
template:
{%- set listkeys = ['degree', 'type', 'location'] %}
{%- for data in profile.get('education_details', []) %}
{%- set nbr = namespace(value=0) %}
{%- for k in listkeys %}
{%- if data.get(k, '') %}{%- set nbr.value = nbr.value + 1 %}{%- endif %}
{%- endfor %}
{%- for k in listkeys %}
{%- if data.get(k, '') %}
{%- set nbr.value = nbr.value - 1 %}
{%- if nbr.value > 0 %}
<span>{{data.get(k, '')}},</span>
{%- else %}
<span>{{data.get(k, '')}}</span>
{%- endif %}
{%- endif %}
{%- endfor %}
<br/>
{%- endfor %}
playtest:
education_details = [{"degree": "B.S.", "location":"aus","grade":"A"},
{"degree": "B.S."},
{"degree": "B.S.","grade":"A"},
{"degree": "B.S.", "type": "Full-type","grade":"A"},
{"type": "Full-type", "grade": "A"},
{"degree": "MCA", "type": "Full-type", "location":"aus","grade":"B"}]
result:
<span>B.S.,</span>
<span>aus</span>
<br/>
<span>B.S.</span>
<br/>
<span>B.S.</span>
<br/>
<span>B.S.,</span>
<span>Full-type</span>
<br/>
<span>Full-type</span>
<br/>
<span>MCA,</span>
<span>Full-type,</span>
<span>aus</span>
<br/>
EDIT
template without namespace:
{%- set listkeys = ['degree', 'type', 'location'] %}
{%- for data in profile.get('education_details', []) %}
{%- set nbr = {'value': 0} %}
{%- for k in listkeys %}
{%- if data.get(k, '') %}{%- set _ = nbr.update({'value': nbr.value + 1}) %}{%- endif %}
{%- endfor %}
{%- for k in listkeys %}
{%- if data.get(k, '') %}
{%- set _ = nbr.update({'value': nbr.value - 1}) %}
{%- if nbr.value > 0 %}
<span>{{data.get(k, '')}},</span>
{%- else %}
<span>{{data.get(k, '')}}</span>
{%- endif %}
{%- endif %}
{%- endfor %}
<br/>
{%- endfor %}
you could use macro:
{%- macro inc(dct, key, inc=1) -%}
{%- if dct.update({key: dct[key] + inc}) %}{%- endif -%}
{%- endmacro -%}
{%- set listkeys = ['degree', 'type', 'location'] %}
{%- for data in profile.get('education_details', []) %}
{%- set nbr = {'value': 0} %}
{%- for k in listkeys %}
{%- if data.get(k, '') -%}{{ inc(nbr, 'value', 1) }}{%- endif %}
{%- endfor %}
{%- for k in listkeys %}
{%- if data.get(k, '') -%}
{{ inc(nbr, 'value', -1) }}
{%- if nbr.value > 0 %}
<span>{{data.get(k, '')}},</span>
{%- else %}
<span>{{data.get(k, '')}}</span>
{%- endif %}
{%- endif %}
{%- endfor %}
<br/>
{%- endfor %}
Related
I have a list of tables and I want to run query using for ... in [] . After running code below I have an error. Where could be mistake?
I just want to print the result. Result I expect in a list for example [2,67,0,7].
tables = [
'table_1'
,'table_2'
,'table_3'
,'table_4'
,'table_5'
]
{%- call statement('my_statement', fetch_result=True) -%}
select count(*) as cnt
from {% for n in tables %} my_schema.n {% endfor %}
where students = 'great'
{%- endcall -%}
{%- set my_var = load_result('my_statement') -%}
{{my_var}}
OR I used it:
{%- set query -%}
select count(*) as cnt
from {% for n in tables %} my_schema.n {% endfor %}
where students = 'great'
{%- endset -%}
{% set results = run_query(query) %}
{{ results }}
Does this do what you want?
{% macro print_multi_tables() %}
{% set tables = ['table_1', 'table_2', 'table_3', 'table_4', 'table_5'] %}
{% set ns = namespace(query_results = [], final_result = '[') %}
{% set query_results = [] %}
{% for table_name in tables %}
{% set query %}
select count(*) from {{ ref(table_name) }} where students = 'great'
{% endset %}
{{ log(query, true) }}
{% set results = run_query(query) %}
{% set count = results.rows[0][0] %}
{% set query_results = query_results.append(count) %}
{% endfor %}
{# This gives a result like [Decimal('2'), Decimal('8')], so #}
{# there is more code below to print the exact results you want #}
{{ log(query_results, true) }}
{# Print the results in the format [result_1, result_2, etc] #}
{% for x in query_results %}
{% set ns.final_result = ns.final_result ~ x %}
{% if not loop.last %}
{% set ns.final_result = ns.final_result ~ ', ' %}
{% endif %}
{% endfor %}
{% set ns.final_result = ns.final_result ~ ']' %}
{{ log(ns.final_result, true) }}
{% endmacro %}
Results will look like
16:44:08
select count(*) from my_database.my_schema.table_1 where students = 'great'
16:44:08
select count(*) from my_database.my_schema.table_2 where students = 'great'
16:44:08
select count(*) from my_database.my_schema.table_3 where students = 'great'
16:44:08 [Decimal('2'), Decimal('6'), Decimal('8')]
16:44:08 [2, 6, 8]
I'm trying to display "notes" in a nested list. Each note has a property called parentID that indicates a note that it is nested under.
Currently I am achieving a single level nest by doing this:
models.py
class Note(Model):
title = CharField()
tags = CharField()
content = TextField()
parentID = IntegerField(default=0)
class Meta:
database = db
app.py
def getSubnotes(note):
note_id = note.id
subnotes = models.Note.select().where(models.Note.parentID == note_id)
return subnotes
app.jinja_env.globals.update(getSubnotes=getSubnotes)
Index.html
<div class="row">
<div class="medium-12 medium-centered">
<ul>
{% for note in notes %}
{% if note.parentID == 0 %}
<li>{{ note.title }}</li>
<ul>
{% for subnote in getSubnotes(note) %}
<li>{{ subnote.title }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
</div>
I would like to, however, recursively find all children notes of a single note and list them out, so there are nests of nests.
I have seen this as an example of how to recursively list in jinja (from jinja docs):
<ul class="sitemap">
{%- for item in sitemap recursive %}
<li>{{ item.title }}
{%- if item.children -%}
<ul class="submenu">{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}
</ul>
However I am confused about what .children actually is. How is it referencing itself or items of the same type?
How would I go about doing this recursively or is there a better method for achieving the same thing?
Any help is very much appreciated!
In the example from the docs, I think that item.children is just an iterable of items of the same type. Then {{ loop(item.children) }} causes the current loop to be executed over the iterable item.children creating a nested list.
You can verify this:
import jinja2
templ = """
{%- for item in items recursive %}
{{item.name}}
{%- if item.children %}
{{- loop(item.children) }}
{%- endif %}
{%- endfor %}
"""
items = [{'name': 'Bobby'},
{'name': 'Jack',
'children': [
{'name': 'Jake'},
{'name': 'Jill'}]},
{'name': 'Babby', 'children': []}]
template = jinja2.Template(templ)
print(template.render(items=items))
prints out
Bobby
Jack
Jake
Jill
Babby
So in your example, I think you should be able to do something like
<div class="row">
<div class="medium-12 medium-centered">
<ul>
{% for note in notes recursive %}
{% if note.parentID == 0 %}
<li>{{ note.title }}</li>
<ul>
{{ loop(getSubnotes(note)) }}
</ul>
{% endif %}
{% endfor %}
</ul>
</div>
as long as getSubnotes(note) returns empty if the note has no subNotes.
Even more complete, in my opinion (it explores lists and dictionaries inside lists):
<dl class="row">
{% for key, value in json.items() recursive %}
{% set outer_loop = loop %}
<dt class="col-3">{{ key }}</dt>
<dd class="col-9">
{% if value is string %}
{{ value }}
{% elif value is mapping %}
<dl class="row">
{{ loop(value.items()) }}
</dl>
{% elif value is iterable %}
<ol>
{% for item in value %}
<li>
{% if item is mapping %}
<dl class="row">
{{ outer_loop(item.items()) }}
</dl>
{% else %}
{{ item }}
{% endif %}
</li>
{% endfor %}
</ol>
{% else %}
{{ value }}
{% endif %}
</dd>
{% endfor %}
</dl>
I wonder if/how could be simplified.
I am currently trying to fix an issue by using jinja variables, but somehow the variable does not keep the value outside the loop, even though I declared it before the loop begins:
{% set disablecounter = 0 %}
{% if disablecounter == 0 %}
{% for einzelroom in all_einzelzimmer %}
{% if zimmer.id == einzelroom.zimmer_id %}
{% set price = einzelroom.preis %}
<div class="preis-element">
<p class="preis"> <span class="smallab"> ab </span> {{ price|int }}€ </p>
</div>
{% set disablecounter = disablecounter + 1 %}
{{ disablecounter }}
{% endif %}
{% endfor %}
{% endif %}
{{ disablecounter }}
The variable is disablecounter inside the loop it is 1 but outside it is still 0
Thanks!
EDIT
Surrounding with a with statement also didnt worked:
{% with foo = 42 %}
{{ foo }}
{% endwith %}
{% with %}
{% set foo = 42 %}
{{ foo }}
{% endwith %}
I found a great solution here on SO by #Chris Warth.
Original answer by #Peter Hollingsworth:
https://stackoverflow.com/a/32700975/5291566
{% with disablecounter = [0] %}
{% if disablecounter == [0] %}
{% for einzelroom in all_einzelzimmer %}
{% if zimmer.id == einzelroom.zimmer_id %}
<div class="preis-element">
<p class="preis"> <span class="smallab"> ab </span> {{ einzelroom.preis|int }}€ </p>
</div>
{% if disablecounter.append(disablecounter.pop() + 1) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
The dictionary I'm passing to the Django template contains 2 dictionaries, each with a list:
'nav_dict': {
'class_name': ['Chemical', 'Avian', 'Mammal'],
'tab_label': ['Chemical!', 'Avian!', 'Mammal!']
}
I want to loop over the lists in each dict to fill out this line of code:
<li class="{{ item_className }} tabSel">{{ item_tabLabel }}</li>
where item_className = each value in the class_name list and item_tabLabel = each value in the tab_label list. The result would 3 <li> tags with a class_name and tab_label.
I have tried something like this (This code only handles the class_name part), but I can't get the loops to append to the same line of code (each <li>):
{% for key, value in nav_dict.items %}
{% if key == 'class_name' %}
{% for item_className in value %}
{% if forloop.counter0 == 0 %}
<li class="{{ item_className }} tabSel">{{ item_tabLabel }}</li>
{% else %}
<li class="{{ item_className }} tabUnsel">{{ item_tabLabel }}</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
The problem you have is with the way you are presenting your data.
Why don't you create your dictionary like this, where each key is the class name and the associated value is the label. It would be more semantic and readable.
'nav_dict': {
'Chemical': 'Chemical!',
'Avian': 'Avian!',
'Mammal': 'Mammal!,
}
Then you can just loop over this and place the key as the class attribute, and the value as the label attribute.
{% for key, value in nav_dict.items %}
{% if forloop.counter0 == 0 %}
<li class="{{ key }} tabSel">{{ value }}</li>
{% else %}
<li class="{{ key }} tabUnsel">{{ value }}</li>
{% endif %}
{% endfor %}
If the order of the elements is important (as dictionaries are un-ordered), you could use an OrderedDict from the collections module to retain insertion order. You would do something like this inside your view...
>>> from collections import OrderedDict
>>> nav_dict = OrderedDict(zip(['Chemical', 'Avian', 'Mamma!'], ['Chemical!', 'Avian!', 'Mammal!']))
OrderedDict([('Chemical', 'Chemical!'), ('Avian', 'Avian!'), ('Mammal', 'Mammal!')])
Similarly you could use a list of tuples as your data
'nav_list': [('Chemical', 'Chemical!'), ('Avian', 'Avian!'), ('Mammal', 'Mammal!')]
And loop over this object like so
{% for class_name, label_name in nav_list %}
{% if forloop.counter0 == 0 %}
<li class="{{ class_name }} tabSel">{{ label_name }}</li>
{% else %}
<li class="{{ class_name }} tabUnsel">{{ label_name }}</li>
{% endif %}
{% endfor %}
If you only want to append an exclamation mark to your string to create the label, you could of course create a custom template filter instead - or just append the exclamation mark inside the template itself.
If you have to use that dictionary, you can use this template code:
{% for item_className in nav_dict.class_name %}
{% with forloop.counter0 as index_className %}
{% for item_tabLabel in nav_dict.tab_label %}
{% with forloop.counter0 as index_tabLabel %}
{% ifequal index_className index_tabLabel %}
{% if index_className == 0 %}
<li class="{{ item_className }} tabSel">{{ item_tabLabel }}</li>
{% else %}
<li class="{{ item_className }} tabUnsel">{{ item_tabLabel }}</li>
{% endif %}
{% endifequal %}
{% endwith %}
{% endfor %}
{% endwith %}
{% endfor %}
I want to change the value of the variable declared outside the loop within a loop. But always changing, it keeps the initial value outside the loop.
{% set foo = False %}
{% for item in items %}
{% set foo = True %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
Ok(1)!
So the only (bad) solution have found so far was this:
{% set foo = [] %}
{% for item in items %}
{% if foo.append(True) %} {% endif %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
Ok(1)!
Ok(2)!
But, its is very ugly! Is there another more elegant solution?
Try also dictionary-based approach. It seems to be less ugly.
{% set vars = {'foo': False} %}
{% for item in items %}
{% if vars.update({'foo': True}) %} {% endif %}
{% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if vars.foo %} Ok(2)! {% endif %}
This also renders:
Ok(1)!
Ok(2)!
as mentioned in the documentation:
Please note that assignments in loops will be cleared at the end of
the iteration and cannot outlive the loop scope.
but as of version 2.10 you can use namespaces:
{% set ns = namespace(foo=false) %}
{% for item in items %}
{% set ns.foo = True %}
{% if ns.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if ns.foo %} Ok(2)! {% endif %}
You could do this to clean up the template code
{% for item in items %}
{{ set_foo_is_true(local_vars) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_foo_is_true(local_vars):
local_vars['foo'] = True
return ''
env.globals['set_foo_is_true'] = set_foo_is_true
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)
This could be generalized to the following
{{ set_local_var(local_vars, "foo", False) }}
{% for item in items %}
{{ set_local_var(local_vars, "foo", True) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_local_var(local_vars, name, value):
local_vars[name] = value
return ''
env.globals['set_local_var'] = set_local_var
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)