The following steps will walk us through using the Jinja2 templating library to create flexible and appealing reporting output:
- Jinja2 is simple and has familiar Python-esque syntax (though it is not Python). Templates can include logic or control flow, including iteration, conditionals, and formatting, which removes the need to have the data adapt to the template. A simple example is as follows:
In [36]: from jinja2 import Template ...: template = Template(u'Greetings, {{ name }}!') ...: template.render(name='Mr. Praline') Out[36]: 'Greetings, Mr. Praline!'
- However, we should decouple our templates from our Python code, and instead, store the templates as text files on our system. Jinja2 provides a central Environment ...