Platform Login

« back to blog overview

Python plot_server

Python plot_server package
A simple and lightweight plot server Python package for data visualisation.

A simple, lightweigtht and ready to use Python package

Purpose - visualise time series data locally

Most IoT data is generated at the edge and collected by an industrial edge controller. This data is often visualised in the backend with solutions such as Grafana. However, in many production scenarios or proof of concepts there is an additional demand to visualise data at the point of origin with a local plot server. Due to the limited memory and CPU resources this mandates a simple and lightweight solution preferably running in memory only. This is why qbee.io created the Python package plot_server that allows anyone to have a local data visualisation running in no time.

We recently had a joint project together with Moxa where the edge device was the UC-2100 industrial gateway with 256MB of RAM. There it was no option to install Grafana locally and have an additional Influx database which stores the time series measurements. Hence, we have developed the plot_server module that is now available as an open source Python package.

Within this joint project with Moxa we were able to read Modbus input signals and send them to the plot server running locally on the edge gateway. We were using qbee-connect to access the device and the plot server running on port 8080.

How it works - introducing the Python plot server

The plot server is an http server written in Python running on a user-defined port (by default 8080). The server has a REST API through which the server can be be fed with payloads through POST requests. These payloads contain the corresponding measurement tag together with the measurement value and an optional unit and timestamp parameter. If the timestamp is left out, then the time of ingestion is used. A sample payload looks like this:

				
					{
    "tag": "Temperature - Room 1",
    "value": 20.3,
    "unit": "\u2103"
    "ts": 1649859909
}
				
			

The server stores the data points of each measurement in memory and in order not to consume the entire available memory after inserting more and more data points the plot server only stores (a configurable) maximum number of points per measurement. Therefore, it is very resource efficient and doesn’t need an additional database running in the background. Also the file system is not utilized. The Python package is built on http.server which is contained in Python’s standard library. The only dependencies are on one additional Python package, namely python-dateutil, making it easy to deploy and allowing for correct timestamps in local browser time.

The entire rendering of the plot happens client-side. If a request is made to the plot server from your web browser, then an HTML file is delivered that contains JavaScript code taking care of the remaining communication with the plot server. The plots are rendered using the famous JavaScript library plotly. The server only sends new data points, the JavaScript resources are obtained from various content delivery network (CDN). The UI is also very lightweight and doesen’t depend on any big frameworks. It is possible to toggle between the various measurements that are stored on the server and you can view updates in real time.

How to get it

Deploying the plot server on your edge device is fairly simple. All you need is a Python installation (>=3.5) together with the package manager pip. Then, you can simply run:

				
					$ pip3 install http-plot-server
				
			

To configure the server you can either supply the following command line parameters 

				
					--host=0.0.0.0
--port=8080
--max-points=100
				
			

or simply put them in a config file called param.cfg (for example) and start the server specifying the path to this file:

				
					$ python3 -m plot_server --cfg=param.cfg
				
			

You can find the package on the Python Package Index (PyPI) and in our GitHub repository.

Looking forward to get your feedback or even a pull-request on GitHub 🙂 We hope you enjoy the package!

Package Info

The package is only 10,3KB large (*.whl file) and contains the following dependencies on the standard library:

Interested to know more?