O'Reilly logo

Practical Data Science Cookbook - Second Edition by Abhijit Dasgupta, Benjamin Bengfort, Sean Patrick Murphy, Tony Ojeda, Prabhanjan Tattar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required