I'm using python
and jinja2
to generate customized reports from the same data using templates (ex: Being able to export to xml
, json
, txt
or custom structure without changing any line of the python script).
I would like the user to modify ONLY the template file to configure its report, and therefore use variables to define the filename and other parameters.
Example template:
{# HEADER -#}
{% set filename = 'custom-report.txt' -%}
{# BODY -#}
{% for item in items -%}
Date = {{ item["Date"] }}
Action = {{ item["Action"] }}
Result = {{ item["Result"] }}
{% endfor %}
From python, is it possible to retrieve the filename value ("custom-report.txt
" in this case) so that I know what's the filename to be used ?
Thanks for your help !
EDIT:
For a better understanding, here is an example of what I expect from python:
template = templateEnv.get_template(template_path)
rawoutput = template.render(items=items)
output_filename = template.getVariable('filename')
with open(output_filename, "w") as stream:
stream.write(rawoutput)
Where the "getVariable" method is what I'm trying to get.