Tested on:RPi3; Raspbian Stretch; Python3
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:Screen, keyboard and mouse

Better RPi remote controls: CherryPy and Jinja for HTML templating

RPi remote controls with Jinja HTML templates for CherryPy

RPi remote controls with Jinja HTML templates for CherryPy

Building remote controls for a Raspberry Pi device with CherryPy is nice, but fiddly. HTML templating with Jinja simplifies building complex websites as remote controls. Basically, HTML templating allows using to external parameters (here: Python variables) and control flow statements like for loops for generating valid HTML. Jinja builds upon the Python webframework Django.
The GPIO of the Raspberry Pi can be accessed from Python as usual.

Templating with Jinja
The most simple example for templating with Jinja is

this creates the string Hello King Arthur!: {{}} marks name as an external parameter and outputs it into the template with its value King Arthur.

For Python3, Jinja is already preinstalled under Raspbian Stretch. The demonstration of Jinja requires CherryPy, a script for a minimal CherryPy server and the demo template:

HTML templating demo
The template index.html contains only

but can be extended by any HTML or JavaScript for complex sites.
CherryPy is a lightweight Python server that’s ready for serving websites with just a few lines of code, see tutorial CherryPy.
Fundamental is the function index of the class MinimumServer

After browsing to localhost, the template index.html from the directory templates is rendered and displayed in the browser. During this, the variable saying is passed to the template with the value Ni!.
Start the server by entering

Now, open a browser and browse to

Jinja Template

Rendered jinja Template

The template has been rendered into a website.

Full script
Click on Full Script to show all of the code.

Import a template into a template
The statement

loads the template file into another template. file can access the same variables as its parent template.

Control flow statements
Control flow statements like for spare repeating HTML code:


Jinja Website: http://jinja.pocoo.org
CherryPy: elegant Python server for the Raspberry Pi: http://www.knight-of-pi.org/cherrypy-an-elegant-python-webserver-for-raspberry-pi-remote-controls/
Django Website: https://www.djangoproject.com
Triggering rc switches with rpi-rf: http://www.knight-of-pi.org/home-automation-with-rpi-rf_send-and-rpi-rf_receive-for-433mhz-rc-switches/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


  1. It is an amazing post. This site is loaded with lots of interesting things, it made it easier for me in many ways.


Time limit is exhausted. Please reload CAPTCHA.