When I initially wrote the car usage tracking web app I hard coded the html into the python script that worked with the data. This is alright for prototyping a D3.js visualization, but it’s a real mess having the page layout and text intermingled with the python code. Luckily there exists a large number of python templating engines that make it easy to fill in variables in an html layout with data from a python script. In this case I used Jinja2.
Here is what part of the python script looked liked before refactoring using Jinja2.
# print info from current fill up and table with all fillups
print "<p>Miles driven: %s" % (float(data[-1][-4]) - float(data[-2][-4]))
print "<p>Your mpg was: <b>%.2f</b>" % float(data[-1][-1])
Jinja2 allows for passing variables from python in the form of a dictionary, where the key in the dictionary is specified in the html template (surrounded by double curly braces i.e. {{ key }}), and will be replaced with the value associated with that key. It allows simple logic such as for loops, which I used to produce the table of past data:
<table>
<tr>
{% for cell in header %}
<td><b>{{ cell }}</b></td>
{% endfor %}
</tr>
{% for row in fillups %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
In the python script, the code to produce the template looks like this:
import jinja2
env = jinja2.Environment(loader=jinja2.FileSystemLoader(
searchpath='templates/')
)
template = env.get_template('mpg.html')
display_dictionary = {}
# code that fills in display_dictionary with the values to send to the template
print template.render(display_dictionary)
It’s also possible to save the rendered template as a static html file:
output = template.render(display_dictionary)
with open('../index.html', 'w') as f:
f.write(output);
Where dispdict is a dictionary containing variables like mpg, milesdriven, etc. The keys to this dictionary are in the template, awaiting replacement the the correct value. The code ends up being much cleaner this way: the data calculation is separated cleanly from the html that defines how it is displayed. Now that I’ve gotten familiar with Jinja2 I plan to use it from the beginning in future projects. I’ll probably further refactor the code to generate a static html file each time the data is updated, since creating the page each time it is visited is not necessary in this case.
The all the code for the car usage tracking web app is up on github (link).