How to do it...

The following steps will walk us through using the Jinja2 templating library to create flexible and appealing reporting output:

  1. 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!' 
  1. 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 ...

Get Practical Data Science Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.