Streamlit — How to build and share your Data Apps on Steroids?
Streamlit is a new and exciting development in the data science community at the moment.
I came across Streamlit the other day when our data science team presented their machine learning results in a web application that looked amazingly professional. I simply couldn’t believe how they could build that data app in so little time (and not having designers and UI people involved…).
They just explained to me that they were using Streamlit to build their demo data app and that this was not a big deal at all. 😏
So, what exactly is Streamlit?
Streamlit is an open-source library, dedicated to the data science and Python community that makes it easy to create and share beautiful, custom web apps for data visualisation, machine learning and data science.
Streamlit was founded by a team of data scientists whose goal is to build the world’s most beautiful tool for machine learning engineers.
The group of founders around Adrien Treuille, Thiago Teixeira and Amanda Kelly were originally working for Google X projects and soon recognised the need for helping data scientists to build and share their work with external audiences.
The recent community hype around Streamlit, as well as its wide adoption lead to a recent investment led by Sequoia. So, it seems as if investors believe that Streamlit’s approach of building and sharing data applications will create a lot of value in the future.
Why is Streamlit great?
Streamlit is beautifully designed and deadly simple to use!
Giving it a try myself, I can definitely confirm what other users said about Streamlit: it’s worth a try, as the time investment is that low that you can write and deploy a beautifully designed, purpose built data analysis application within two hours and with less than 100 lines of Python code.
Why to deploy with Docker?
Don’t waste time with a local Streamlit installation, use Docker right away. I have to admit that I was too stupid to get it running on my Windows laptop nor on my iMac. Yes, I know about using different Python environments, etc… but I hate the complexity and I hate different library version dependencies even more. 😠
I struggled with several dependency and Python environment issues, until I realised that its much easier and without hassle to develop and deploy the Streamlit app within a Docker container right from the beginning.
To run a Streamlit application within a Docker container you just need to create a Dockerfile that is derived from the base Python Docker image, as shown below:
FROM python:3.7
EXPOSE 8501
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD streamlit run app.py
Don’t forget to put the Python library requirements.txt file into the same folder, as it is shown below:
streamlit
Finally, implement your Streamlit application within the app.py file.
I already added two Streamlit widgets, one for showing a chart with some random time series data in it as well as a geographic map widget that shows random longitude and latitude locations in the Bay area.
import streamlit as st
import numpy as np
import pandas as pd st.title('Dimensional Data Analysis')
chart_data = pd.DataFrame( np.random.randn(20, 3),
columns=['a', 'b', 'c'])
st.line_chart(chart_data)map_data = pd.DataFrame(
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
columns=['lat', 'lon'])
Now lets build the Docker container by typing in following command:
$ docker build -f Dockerfile -t app:latest .
Finally, run your Streamlit application by starting the previously composed Docker container:
$ docker run -p 8501:8501 app:latest
Now you can access your Streamlit data dashboard through your local HTTP address on the exposed port 8501.
Just open your browser and navigate to ‘localhost:8501’ which will lead you to your newly created Streamlit data dashboard, as it is shown below:
The nice thing about Streamlit is that you don’t need any interaction code nor visualisation markup or styling. Everything is already built in, along with options to conveniently zoom all charts and maps as well as to export any chart into any kind of format such as CSV or images.
This saves a tremendous amount of time and it lets you focus on the essence of your work, which is not to stumble around with css files and thousands of JavaScript frameworks, but much more to focus on delivering value through data visualisation and analysis.
How to share your Data App?
Finally, coming to the ultimate benefit of having the Streamlit application built as a Docker container right from the start, which is to share and to deploy your app on share.streamlit.io.
Streamlit sharing for now is on invite only, but you can easily request an invite here.
In order to share and deploy your app you just have to follow these 3 simple steps:
- Create a public GitHub repository and put those 3 files of your app into that repository.
- Sign into share.streamlit.io
- Click ‘Deploy an app’ and then paste in your GitHub URL
Publish your app on Docker Hub
Besides hosting and running your data science app on the Streamlit platform, you can also push your complete application code to GitHub and then use GitHub actions to automatically publish your data application on Dockerhub.
Having your application registered at a Docker registry, such as Dockerhub allows any user to pull and run your data app within any kind of Docker runtime environment, no matter if you are using a SaaS Cloud such as AWS Fargate, Google GCP, Azure or a local Kubernetes cluster.
What about the license?
As the Streamlit library comes with an Apache 2 license, you are free to use and deploy your data app that is built on it.
Now you could ask how the Streamlit company is planning to build a business model on top of that great framework?
So far, there is no hint on how they plan to commercialise the value they are providing to the data science community.
I could only guess that they will start to price commercial hosting of data apps in their cloud. As Docker makes the sharing and the deployment of Streamlit apps deadly simple, commercialising the hosting will become a hard task for them.
Summary
Streamlit is definitely worth the time to learn another app framework in Python.
It allows you to build and deploy beautifully designed data apps within several lines of Python code.
By using Docker right away, you save a lot of time and effort and you are up and running your own company’s data apps within hours instead of investing precious time and money.
Thanks a lot for reading, I hope my article helped to build and deploy your own Streamlit data app.
Follow me on Medium to keep up to date with any future articles!