Getting started with Python’s powerful Streamlit framework with a simple example

Streamlit is one of the most powerful and well-packaged Python libraries I have come across in a while. I first stumbled upon the software, or a framework as it’s creators like to call it, through a post on LinkedIn and was immediately intrigued by what it had to offer.

Described as a framework for deploying visualizations and dashboards as apps, it is primarily geared towards use-cases of data science and machine learning. However, it is much more than a simple Python library; it’s an entire ecosystem which incorporates other libraries and offers a plethora of features. Hence, the creators are accurate in calling it a framework.

For a while, I was looking for a way to deploy some analysis or a dashboard via an app that could easily be viewed by anyone on a browser. Streamlit provided the perfect solution for that!

Below is a glimpse into what the app looks like when fully deployed, using some analysis that I performed with the 2018 FIFA World Cup squads dataset. I will cover this example in more detail later.

To access the app, you can use this link OR head to my Github repository and click on the black ‘Open in Streamlit’ button (as shown below) in the description for a cooler experience.

I like to think of Streamlit as more similar to a Jupyter Notebook or an Anaconda than a Pandas, if that makes sense. It provides an entire software ecosystem which works with Python. The documentation on the website is well-structured and provides step-by-step guidelines for it’s implementation and deployment as an app, as well as it’s seamless integration with the Python environment.

Below, I describe the steps required to successfully deploy a Streamlit app along with details regarding my analysis of the 2018 World Cup squads. Let’s get right into it!

Python scripting

The first step towards creating a Streamlit app is to create a Python script which performs certain tasks. In this case, I explored the 2018 world cup dataset and created some visualizations and markdown text using Streamlit functions.

Similar to installing most packages in Python, Streamlit can simply be installed using the pip install streamlit command. The script used for the analysis can be found in the Github repository linked above along with the dataset and the requirements.txt file which contains the libraries required to support the analysis in the script.

The analysis is split into three main sections:

  1. Displaying a snippet of the dataset
  2. Analyzing the Caps and Goals variables using subplots
  3. Analyzing the Position variable using a bar chart

We will focus on two specific Streamlit functions:

  • Write: Used to create markdown text
  • Pyplot: Used to generate plots using the matplotlib framework

A snippet of the first section is shown below. Let’s break this down.

We first import all the necessary libraries followed by the setup of a gridspec scheme. For anyone who is unfamiliar with the gridspec function, it is a useful tool part of the matplotlib library useful for customizing subplots.

Lines 13-15 show the first usage of a Streamlit function. In this case, the write function is used to create markdown text, which would eventually be portrayed in the final app. Line 21 contains another write function which contains a combination of some markdown text and the loaded dataframe. In this case, the first five rows of the dataframe would be displayed in the app which is a great feature of this framework.

In the second portion of the script, I visualized the Caps and Goals variables using histograms and scatterplots in a 2×2 grid. Once again, the header for the section is written in markdown on line 23.

On line 21, we are now introduced to a new Streamlit function called pyplot which supports the matplotlib framework. For example, if a new figure is defined in matplotlib using a command of the following nature,

fig3 = plt.figure() OR

fig3 = plt.figure(constrained_layout=True,figsize=(21,11))

the figure variable can be used within the pyplot function to generate the plot in Streamlit’s environment. A command of that nature is executed on line 59 of the code. The rest of this section mostly comprises standard matplotlib functions. The third and last section once again utilizes Streamlit’s write and pyplot functions to create some more markdown text and a bar chart displaying the frequency for each position in the dataset.

In order to use the Streamlit framework, the script needs to be run using a streamlit run command as opposed to the usual python command in Anaconda. For example, I executed the following command to run my script which opened up a preview of the app in a web browser:

Sign up for Streamlit sharing and link Github account

On the preview page, click on the menu button (3 stripes) on the top-right corner of the page and select ‘Deploy this app’.

This will open up a window which will prompt you to link a public Github repository. However, there is another key step that is not completely obvious from the instructions, which is to sign up for Streamlit sharing.

Clicking on ‘our documentation’ on the above window will take you to the Streamlit website which provides further instructions on how to deploy the app. However, there are only two key items to do at this point:

  1. Place your code, data and requirements.txt file in a public Github repository
  2. Sign up for Streamlit sharing and link the above Github repository

Signing up for Streamlit sharing is simple. Once on the page (shown below), click on ‘Request an invite’ and follow the instructions to complete the setup. The two crucial pieces of information required are the Github repo directory and email address linked to the repo.

Once you receive an email confirmation that your account is setup, the app is ready to be deployed!

Deploy the app

Sign in to your Streamlit sharing account and click on the blue ‘New app’ button on the top-left of the page. Then fill out the information required to deploy the app. Once your Github account is linked to Streamlit, this step is fairly intuitive.

Hit ‘Deploy!’ when you are done and Streamlit will start executing your Python script using the libraries mentioned in the requirements.txt file.

Note that during this initial deployment, it takes a bit of time to get all the necessary libraries installed. However, once live, re-running the app takes only a few seconds and each time anyone visits the page, the app automatically re-runs.

If the app has been inactive for over 5 days, which means any interaction from the creator or other users, an email from Streamlit will notify you that it’s about to put the app to sleep. However, the app can be rebooted from your Streamlit account’s dashboard.

There you go! I hope this was useful. Let me know in the comments if you have used Streamlit before and what other features of this software you have found useful. My eventual goal is to deploy a finance tracking dashboard that I had created, using Streamlit, in order to make it more accessible to other users. Keep an eye out for that!