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 pythonprogramming.net, 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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import dash from dash.dependencies import Input, Output import dash_core_components as dcc import dash_html_components as html import datetime import random app = dash.Dash() app.layout = html.Div([ html.Link( rel='stylesheet', href='/static/stylesheet-oil-and-gas.css' ), html.H3('Dash Example: Updating several DIVs at regular intervals'), html.Div([ html.Div(id='my-output-interval', className='three columns', style={'color': 'blue', 'fontSize': 20, 'border-width': '2px', 'border-style': 'solid', 'border-color': 'blue', 'border-radius': '5px'}), html.Div(id='my-temperature-sensor', className='three columns', style={'color': 'red', 'fontSize': 20, 'border-width': '2px', 'border-style': 'solid', 'border-color': 'red', 'border-radius': '5px'}), html.Div(id='my-distance-sensor', className='three columns', style={'color': 'green', 'fontSize': 20, 'border-width': '2px', 'border-style': 'solid', 'border-color': 'green', 'border-radius': '5px'}), html.Div(id='my-humidity-sensor', className='three columns', style={'color': 'orange', 'fontSize': 20, 'border-width': '2px', 'border-style': 'solid', 'border-color': 'orange', 'border-radius': '5px'}), ], className='row'), dcc.Interval( id='interval-component2', interval=4*1000, # in milliseconds n_intervals=0), dcc.Interval( id='interval-component', interval=1*1000, # in milliseconds n_intervals=0), ], style={'marginBottom': 50, 'marginTop': 25, 'marginLeft': 25, 'marginRight': 25}) @app.callback( Output('my-output-interval', 'children'), [Input('interval-component', 'n_intervals')]) def display_output(n): now = datetime.datetime.now() return 'It is {}:{}:{}'.format( now.hour, now.minute, now.second ) @app.callback( Output('my-temperature-sensor', 'children'), [Input('interval-component2', 'n_intervals')]) def display_output(n): now = datetime.datetime.now() return 'Temperature is {}'.format( random.randint(23,34) ) @app.callback( Output('my-distance-sensor', 'children'), [Input('interval-component', 'n_intervals')]) def display_output(n): now = datetime.datetime.now() return 'Distance is {} cm'.format( random.randint(30,80) ) @app.callback( Output('my-humidity-sensor', 'children'), [Input('interval-component2', 'n_intervals')]) def display_output(n): now = datetime.datetime.now() return 'Humidity is {} %'.format( random.randint(70,98) ) if __name__ == '__main__': app.run_server(port=8030, debug=True) |
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.