[PYTHON] [Jinja2] Solution to the problem that variables added in the for statement are not inherited

Introduction

Python: 3.7.4 jinja2: 2.7.2

Suppose you have the following code and you add a variable called cnt in the for statement.

count_test.j2


{%- set cnt = 1 -%}
{%- for i in range(3) -%}
{%- set cnt = cnt + 1 -%}
{{ cnt }}
{% endfor -%}
result : {{ cnt }}

Execution result (failure example)

Looking at the result, you can see that the result added to the variable is not inherited outside the for statement.

output


2
3
4
result : 1

Depending on the version, it seems that it may not be added.

output


2
2
2
result : 1

Solution 1: Store in list

It works well if you store the added result in a list. Add the added value to the list with append, and delete the value before addition with pop.

count_test2.j2


{%- set cnt = [1] -%}
{%- for i in range(3) -%}
{%- set _ = cnt.append(cnt[0] + 1) -%}
{%- set _ = cnt.pop(0) -%}
{{ cnt[0] }}
{% endfor -%}
result : {{ cnt[0] }}

Solution 2: Use namespace

<2019/12/21: From na90ya> It seems that variables are preserved when using namespace objects introduced in 2.10 of jinja2. (Reference material [1])

count_test3.j2


{%- set ns = namespace(cnt=1) -%}
{%- for i in range(3) -%}
{%- set ns.cnt = ns.cnt + 1 -%}
{{ ns.cnt }}
{% endfor -%}
result : {{ ns.cnt }}

Execution result (success example)

Looking at the result, the variables are still added outside the for statement.

output


2
3
4
result : 4

Reference material

[[1]Template Designer Documentation] (https://jinja.palletsprojects.com/en/2.10.x/templates/#assignments)

Recommended Posts

[Jinja2] Solution to the problem that variables added in the for statement are not inherited
A solution to the problem that files containing [and] are not listed in glob.glob ()
Solution to the problem that Ctrl + z cannot be used in Powershell in Docker for windows environment (provisional)
A solution to the problem that the Python version in Conda cannot be changed
Solution to the problem that build does not end when installing OpenCV (PEP517)
Solution to the problem that you can't activate by putting conda in pyenv
[Python] Solution to the problem that elements are linked when copying a list
How to define multiple variables in a python for statement
Try to extract the keywords that are popular in COTOHA
How to write type hints for variables that are assigned multiple times in one line
How to turn the for statement when there are multiple values for one key in the dictionary
[VLC] How to deal with the problem that it is not in the foreground during playback
Solution to the problem that the display is corrupted when the .exe command is included in the while loop in wsl2
Upload and manage packages that are not in conda to anaconda.org
[Introduction to Python] How to use the in operator in a for statement?
Solves the problem that static files (CSS, JS, img) are not read when DEBUG = False in Django.
The NVM Checksum Is Not Valid, a solution to the problem that Intel's wired LAN is not recognized on Linux
Change the list in a for statement
I thought it would be slow to use a for statement in NumPy, but that wasn't the case.
How to find the correlation for categorical variables
Note that I was addicted to npm script not passing in the verification environment
About the matter that the contents of Python print are not visible in docker logs
[systemd] How to deal with the problem that fancontrol does not work after suspending
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
Solve the problem that CSS is not reflected during web application development in Flask
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
How to run the Ansible module added in Ansible Tower
Key input that does not wait for key input in Python
Convenient to use matplotlib subplots in a for statement
When you want to plt.save in a for statement
Do not pass self to ProcessPoolExecutor in the class
The story that yapf did not work in vscode
[Linux] How to monitor logs that are constantly added
ABC's A problem analysis for the past 15 times to send to those who are new to Python
Is it a problem to eliminate the need for analog human resources in the AI era?
How to solve the problem that video content cannot be played on Firefox for Linux
How to set variables that can be used throughout the Django app-useful for templates, etc.-
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows