2

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.

1 Answers1

1

UPDATE

You'd do that likewise you returned your items in your template. In your context just add a variable filename = 'custom-report.txt' # or whatever you need and on the rendering you just use {{ filename }} to display it.

A Simple example with jinja2 package

import jinja2
t = jinja2.Template("This is the filename: {{ filename }}!")
print t.render(filename ="custom-report.txt")

The template could also be your file.

So you need python to read a variable from the template. That cannot be done without a framework i.e. flask that has a request object to pass values. A solution I suggest is to read your template in python and parse it to find the text you need. i.e. With the above template given as is

template = templateEnv.get_template(template_path)
rawoutput = template.render(items=items)

output_filename = open('a.txt', 'r').read().split('{% set filename = \'')[1].split('\'')[0]
print(output_filename)

with open(output_filename, "w") as stream:
    stream.write(rawoutput)

That way you parse the whole template and split the filename. Not the best method but works if you keep a standard format.

Community
  • 1
  • 1
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • Thanks, I know how to use variables inside a template. The goal for me is just to parse the template from python and retrieve some variables contained in it, so that I can adapt the action that will follow the rendering. – Sébastien Matton Aug 23 '19 at 09:37
  • I also updated my first post with an example, so you can better understand what I'm trying to do. The filename "custom-report.txt" is NOT known by the python script! – Sébastien Matton Aug 23 '19 at 09:55
  • @SébastienMatton Take a look above. – Kostas Charitidis Aug 23 '19 at 10:08
  • Thanks. That was indeed the way I would have worked if there was no more elegant solution...But I'm still crossing my fingers and hoping there is a real solution that won't be affected by extra spaces, simple or double quotes,... :-) – Sébastien Matton Aug 23 '19 at 10:28
  • Without a client-server framework passing variables that cannot be done because python renders the file with its own data, but the reverse cannot be done. Hope i helped. – Kostas Charitidis Aug 23 '19 at 10:37