A few days ago I received my new Beaglebone Black. Since then I spend much time on playing around with it. I installed a Teamspeak server, connected it with my old Beaglebone and my Raspberry Pi in a grid structure and wrote a simple service to monitor some sensors connected to the Beaglebone.
During the development of the sensor service, I had to face the problem of providing the sensor data in different formats, so I could access the data from different platforms using a very basic REST interface. The simplest solution to this problem is using the Preppy library.
The Preppy preprocessor
Preppy is a simple preprocessor that is maintained by ReportLab. Using Preppy it is incredibly easy to write templates that could be filled with arbitrary data. You just create the basic structure of your document and embedd Python expressions and control structures into it.
You could think of a Preppy template like a normal Python function which is called using parameters and return the filled template as result. Thus, Preppy compiles a template to a Python module (*.pyc file), which you then call with the needed parameters.
Installing Preppy is pretty simple using pip, which is a tool for installing and managing Python packages. To install pip just run:
Using this tool is as simple as using apt-get. Thus, you just have to run
to install Preppy.
A simple example
The following example shows a simple json template
which will be translated by Preppy to the json file below:
This example may some of you remind of somehow similar templates using PHP:
However, to compile the above template using preppy, you just create a module using preppy.getModule() and fetch the json content by calling the get() method of the module with the parameters defined within the template. Finally open a file handle and write the content to a new *.json file and you are done.
You see, using Preppy it is pretty easy to provide data in different formats. Using templates like the one above, I was able to implement a simple REST service, that provides the output of the sensors connected to my Beaglebone Black in JSON, YAML, XML and CSV format. And in case I would like to publish the sensor output over a web interface, I just have to add a HTML template and I’m done.
Hope you enjoyed this short introduction to Preppy. In my next posts I will cover the usage of Preppy in the context of a REST service and the automated generation of reports using ReportLab, z3c.rml and Preppy. So stay tuned and until next time, happy coding.