Python Automation Cookbook

Python Automation Cookbook

Using templates for reports

HTML is a very flexible format that can be used to present rich reports. While an HTML template can be created by treating it as just text, there are tools that allow you to add better handling of structured text. This detaches the template from the code as well, separating the generation of the data from the representation of that data.

Getting ready

The tool used in this recipe, Jinja2, reads a file that contains the template and applies the context to it. The context contains the data to be displayed.

We should start by installing the module:

$ echo "jinja2==2.20" >> requirements.txt
$ pip install -r requirements.txt

Jinja2 uses its own syntax, which is a mixture of HTML and Python...

Learn for FREE 5 days of free access
Unlimited access to the entire Packt Library from 24th - 28th July. No credit card required
Learn for FREE 5 days of free access
Unlimited access to the entire Packt Library from 24th - 28th July. No credit card required

Your notes and bookmarks