I am working on a Python 3 project and besides some TK window output, I'd like to generate HTML. While I could do that with string operations, I don't think that this will be a easy to maintain and easy to adapt by users.

I know that there is a list of templating packages, but that list is rather long and it mentions packages that are outdated, not suitable for Python 3 etc.

In order to minimize my trial-and-error approch, I am looking for a recommendation with the following properties:

  • works in Python 3
  • works on Raspberry Pi
  • package is actively maintained
  • support for loops (for) and branches (if).
  • easy to understand for developers that already know HTML and CSS

My personal recommendation would be Jinja2. The templating language has a syntax that's not particularly hard to learn if you're already used to coding in Python and have some experience with HTML, it's wicked fast, has a number of useful features for working with HTML right out of the box, it's pure Python with minimal dependencies (if I remember correctly, there's only one outside of the Python standard library), and it's packaged for pretty much every major distribution (including Raspbian).

The only potential downside is that Python 3 support is still listed as experimental and needs Python 3.3 or newer, though the initial Python 3 support was added quite some time ago (they don't even support the version that originally added it anymore, so I think it's been at least six months), they've pretty much said they're just being conservative (all the tests pass on Python 3, they're just not 100% certain that there aren't any lurking corner cases that break on Python 3), and I've personally had no issues using it with Python 3.

Django's templating engine is pretty widely used and supports Python 3, but it's not exactly lightweight (you pretty much have to install all of Django to get the templating engine), and they tend to break things in subtle ways version to version. Other than that, it's got most of the same benefits that Jinja2 does (Jinja2 was actually originally a standalone implementation of Django's templating language, but it's diverged enoguh that Jinja2 templates and Django templates aren't guaranteed to be interchangeable).

The only other templating system mentioned in your link that I actually recognize is Mako. I've never dealt with it myself, but have heard from a number of associates that it's reliable and I've seen a number of big Python projects using it (I think Reddit uses it actually). The syntax is a bit closer to HTML or XML than Jinja2 and Django are, but that can be just as much of an issue as a benefit (the closer the syntax, the easier it is to mix up what's part of the template and what's actual HTML).


I don't know if it works on Rasberry Pi specifically (but I don't see why it wouldn't), but Cheetah covers your other requirements. I used it for a substantial project and was happy with it. I especially liked the ability for a template to inherit from another and to easily augment its functionality with Python code when necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.