Jinja2 as a Command Line Application

The most common use of Jinja2 is in web applications, where it is used to create HTML files from template files. But I have used it outside web applications too.

I have used it to create images using SVG. I have an SVG template. I add or remove segments or text in that using Jinja2 templating language. So for example you want to create labels or certificates, you can do this.

I also create student report cards using Jinja2. I use data from CSV, create a custom HTML report using Jinja2, and then convert that into a beautiful PDF report using WeasyPrint.

I also have some Jinja2 templates that take some variables and create a bunch of SQLs to achieve something. Mostly if I am doing customer support.

As you can see, I use Jinja2, where I have data and a template and am expected to produce text output. In most cases, it would be a simple python script like this.

from jinja2 import Template
template = Template(TEMPLATE)
print(template.render(data=data))

Suppose I could call a CLI tool that takes data JSON (or any other format) and a template file. And outputs the rendered text or file. Then I can avoid writing this python script. I can also use piping from the UNIX world to mix with other UNIX tools.

This is where jinja2-cli package comes into the picture. It's a python package, once installed, brings the power of Jinja2 library to a command-line tool called jinja2.

 pip install jinja2-cli

The command takes a template file and a data file. And returns the rendered file or prints to STDOUT.

jinja2 takes template and data, and outputs rendered content.
jinja2 [options] <input template> <input data>

It can take data in the form of JSON, XML, YAML, querystring, ini, auto, key=value pairs etc. That's almost any kind of data.

For example, I can render the email.html template using the data from data.json and pipe it to swaks to email me.

jinja2 email.html data.json | swaks --to i@thejeshgn.com --tls --body -

Here is something that is more practical and I use on daily basis. I pull the data from an RSS feed. I format it and send the rendered HTML as an email.

xidel https://www.thisiscolossal.com/feed/ \
--xpath="//item/title" --xpath="//item/link" \
--output-format=json-wrapped \
--output-header='{ "data":[' \
--output-footer=']}' \
--silent > data.json; \
jinja2 email.html data.json | \
swaks --header "Subject: Reading list for Colossal" \
--to i@thejeshgn.com --tls \
--add-header "Content-Type: text/html" \
--body - 

It has three parts. The first part xidel gets the RSS feed and converts them into simple JSON and stores it inside data.json.

xidel https://www.thisiscolossal.com/feed/ \
--xpath="//item/title" --xpath="//item/link" \
--output-format=json-wrapped \
--output-header='{ "data":[' \
--output-footer=']}' \
--silent > data.json;

In the second part jinja2 renders the template and outputs to the STDOUT

jinja2 email.html data.json

In the final and third part swaks reads piped data and sends the HTML email to me

swaks --header "Subject: Reading list for Colossal" \
--to i@thejeshgn.com --tls \
--add-header "Content-Type: text/html" \
--body -

The email template looks like this

<html>
<body>
    <h2> Headlines </h2>
<ol>
{% for item in data[0] %} 
    <li><a href="{{data[1][loop.index-1]}}">{{item}}</a></li>
{% endfor %}
</body>
<ol>
</html>

JSON produced by xidel looks like this

{ "data":[[
  "Celebrating Kenyan Culture, Bold Textile Patterns Disguise Subjects in Thandiwe Muriu’s Portraits",
  "Illuminated Dinosaurs Stalk Paris’s Jardin des Plantes in a Spectacular Journey Through Time",
  "A New Book Captures Roger A. Deakins’s Signature Cinematic Style Through Ironic Black-and-White Photos",
  "Cut from Found Feathers, Minuscule Silhouettes Become Intricate Symbolic Works",
  "Jane Goodall, Paul Nicklen, and 100 Photographers and Conservationists Join a Print Sale to Protect the Environment",
  "Bars of Light Pierce a Dilapidated Sydney-Area Home in Ian Strange’s Illuminated Intervention",
  "December 2021 Opportunities: Open Calls, Residencies, and Grants for Artists",
  "Canning the Sunset: Hundreds of Jars of Dyed Sand Preserve the Swirling Colors of a Skyline Before Dusk",
  "Vintage Fabrics Encase Ceramic Shards in Zoë Hillyard’s Mended Pottery",
  "Two Curious Rats Endure the Disastrous Effects of an Experiment Gone Haywire in an Animated Short"
], 
[
  "https://www.thisiscolossal.com/2021/12/thandiwe-murius-portraits/",
  "https://www.thisiscolossal.com/2021/12/dinosaur-exhibition-paris/",
  "https://www.thisiscolossal.com/2021/12/roger-a-deakins-byways/",
  "https://www.thisiscolossal.com/2021/12/chris-maynard-feathers/",
  "https://www.thisiscolossal.com/2021/12/vital-impacts-print-sale/",
  "https://www.thisiscolossal.com/2021/12/ian-strange-light-intersections/",
  "https://www.thisiscolossal.com/2021/12/december-2021-opportunities/",
  "https://www.thisiscolossal.com/2021/12/carly-glovinski-sand-sunsets/",
  "https://www.thisiscolossal.com/2021/12/zoe-hillyard-ceramic-patchwork/",
  "https://www.thisiscolossal.com/2021/12/experiment-animated-short/"
]]}

You can read this blog using RSS Feed. But if you are the person who loves getting emails, then you can join my readers by signing up.

Join 2,240 other subscribers

1 Response

  1. December 17, 2021

    […] you can also use it as a CLI. For example, you can use jinja-cli or yasha. They take a data file and a template file to generate the final document. For instance, I […]