Last year, Posit (formerly RStudio) released the “Shiny for Python” framework. If you are a data scientist, you probably know how good the R Shiny framework is.
In Python, there are multiple web frameworks and there are a few specialized in data analytics. However, I think shiny is different and has a lot more potential than other alternatives such as Streamlit or Dash.
Considering all the experience Posit has with R-Shiny, the framework was open sourced in 2012, I would expect the development of the Python-Shiny to be a lot faster.
When should I use Streamlit, Dash or Gradio?
If you are building a simple demo app or an app with a few plots and limited interactivity I think streamlit and a “script” approach is probably the best.
For example, let’s say you have a forecasting model you want to demo. In this situation you might have the following inputs:
- Date Range
- Some other values associated with region or product
- A table showing the model results
- A plot with the true values and the model predictions.
In this case, I think the logic is relatively simple. You have a few inputs and outputs. The interactivity is also limited.
When should I use Shiny for Python?
If you want to develop more complex web application with multiple moving parts and these components interact in a complex way.
When you have a complex relation between inputs and outputs, I think shiny for python is probably the best option today in Python.
Another advantage is when you are working with a relatively large dataset. In shiny, the data gets read once. No matter if you are reading a local file or querying a database. In the “script” approach, the data is read each time a change is made.
How to get started with Shiny for Python?
Probably the best way to get started, if you have already used shiny in R is taking a look at the shinylive examples.
shinylive apps run entirely in the browser! This is just amazing, you basically don’t even need a server to run Shiny. However, this doesn’t work for all use cases.
If you have used shiny in R, then the Python version should look very familiar. In you are just getting started with the Python version, I explain how to go from zero to a working application in the Shiny for Python video series.
1. First Video – Basic Finance Application
Start with this video to develop a simple first version of a realistic web application with Shiny for Python.
2. Second Video – More Complex Application
In this video, I explain how to go from a relatively basic application to a more professional looking application.
This app includes two pages, has multiple interactive plots (plotly based) and a more complex back-end.
Set up local development environment for shiny-python
In order to follow the tutorial, you will need to have shiny installed in an anaconda environment.
conda activate envname
pip install shiny==0.3.0
pip install pandas==2.0.0
pip install plotly==5.12.0
pip install shinywidgets==0.1.6
pip install matplotlib
Once you have a script with a shiny app locally, you can run it using this command from the terminal:
shiny run --reload 01-app.py
This is how it looks running the app I covered in the YouTube from VSCode.
To run the second (more complex) application run this command in the terminal.
shiny run --reload 02-app.py
If you want to run the project locally on your computer, check out the shiny-python github project where you can find the source code and data I used to build both applications.
Shiny for Python is a great new addition to the data analytics ecosystem. It allows data analyst and data scientists to build complex interactive web applications easily.
To get started you can check the examples in the shinylive website and follow the hands-on video tutorial. Good luck!!