Python Dash!

Let’s go to the point: Finally Python has what R used to have since a looong time ago with Shiny. This is nothing less than Python Dash, a Python framework for building  analytical applications (in the author’s own words).

The library is based around Flask and Plotly, the installation is pretty easy and it is described very well here. There are plenty of tutorials and an application gallery full of examples. I would really like to say thanks to sentdex (Harrison Kinsley) of,   for his great series of 12 tutorials on the subject of Python Dash. He culminates with a live web based application, which performs real-time sentiment analysis of tweets. The app is available here.

Shiny has as proven record, it has been taught in many courses, MOOCs, tutorials and it has proven itself worthy. It has a big user base. Shiny apps can be deployed internally and externally on the web. But how good is Dash in comparison? And what can you do with it outside of the normal beaten track, this is what you should think in my opinion when evaluating such libraries. Do they give an edge?

Python Dash has two sets of components that can be used to compose the layout of a web application. The first, contained in the package  “dash_core_components” are more specific to analytics. The other “dash_html_components” are basically all the possible HTML tags. Callbacks can be connected to fields events (such as modifications of a text box on the web page) and time intervals (such as a regular event which occurs every X milliseconds).

The callbacks and the events are associated by annotations on the callback functions.

The apps in the gallery look definitely great, and you can definitely understand very easily the potential. Deployment of the application on an apache2 web server is explained by sentdex in his tutorial, part 11. This shows that the frameworks can very easily scale “upwards”.

however I was more thinking about how easy would it be to build a web page that would display sensor data from a Raspberry Pi for instance with regular intervals. For the moment no analytics, no graphs. That is, I would like to see how well it scales “downwards”.

The first thing to see is if it runs “at all” on the RPI. And the answer is YES! All libraries can be installed without a hitch. Using Thonny, I have produced the following code, this at the moment does not read the actual sensors… …in fact it does nothing BUT!For instance the distance sensor part. It can be easily extended with the code contained in my other blog, here. Sorry, that is in French but the Python code is… just Python.

The app  includes a Bootstrap-like style sheet, it  contains TWO different time intervals (could not find this on the net) and runs very well on a RPI model 3B!

And that was it! As you see I am doing very easily all HTML and css (I am using this file from one of the apps in the gallery) in a single Python file, no HTML files, no javascript, everything in pure Python. The callbacks are connected to two intervals, defined in the list of components that define the app.layout (they are part of the dash_core_components package). The callbacks are simply the annotated functions (annotated with @app.callback).

Well, I guess that the power we have here is more or less equivalent to Shiny, (see my app guessnextword which was part of my Data Science Capstone project) but… …until last year Dash was not there, so you had to put a lot more effort to do the same with Python! And now, with the wealth of libraries in Python, you can imagine a number of applications easily written in Dash, so… …Welcome Dash!

BTW, that is the look of the little app in Google Chrome.