- What is Streamlit?
- Prerequisites
- Installing Streamlit
- Setting up your Streamlit workspace
- Creating your first Streamlit app
Streamlit is a Python library that you can use to build interactive data-driven web apps.
A typical workflow for the creation and deployment of Streamlit app is summarized below:
- Collect requirements - In this phase, we want to make a list of the desirable features and capabilities that we want our web app to do.
- Code - Next, we'll do the actual coding of the web app with the Streamlit library.
- Repo - Once the code is complete, we can Git push (i.e. upload) it to a GitHub repo where app files are stored and used in a subsequent deployment phase.
- Cloud - To share our Streamlit apps publicly, we're going to deploy the app on the cloud using a platform such as Streamlit Community Cloud. In a few clicks of the mouse, one can go from app files stored on the GitHub repo to a deployed app.
- App - Streamlit app can be easily shared via the deployed URL in the format of https://.streamlit.app/ where
subdomain
refers to the unique identifier that can either be automatically generated by the server as a long form URL (e.g. https://dataprofessor-st-matplotlib-line-plot-streamlit-app-2a0abu.streamlit.app/) or explicitly renamed to a shorter and easy to remember one (e.g. https://chanin.streamlit.app/).
Here's what you need to use Streamlit:
- Have basic Python knowledge.
- Write scripts to perform specific tasks (like taking several Excel files as input and combining them into one).
- Build and grow the Streamlit app line by line instead of starting with a predefined layout (it takes only a few lines of code). If you can do all this, congratulations! You're ready to plunge into the world of Streamlit.
If you already have an existing Python coding environment, Streamlit can be installed using pip
as shown below:
pip install streamlit
It is typically good practice to house the Streamlit app in their own dedicated conda environment. This way the library dependencies don’t get entangled with other Python libraries used by other apps.
Here, we're going to replicate a Streamlit app from an existing GitHub repo available at https://github.com/dataprofessor/eda-app/.
Particularly, we're going to clone the EDA app from a YouTube tutorial video on How to Build an EDA app using Pandas Profiling.
Step 1. Create a conda environment
Create a conda environment called eda:
conda create -n eda python=3.9
Step 2. Activate the eda environment:
conda activate eda
Step 3. To install prerequisite libraries we must first download the requirements.txt file (it contains the library version numbers):
wget https://raw.githubusercontent.com/dataprofessor/eda-app/main/requirements.txt
Step 4. To actually install prerequisite libraries using the requirements.txt file
pip install -r requirements.txt
Inside the requirements.txt
file you'll see the following contents:
streamlit
pandas
numpy
ydata-profiling
streamlit-pandas-profiling
Jinja2
You'll notice that in the above contents, we're using legacy versions of the Python libraries, which is to be expected as this is from a tutorial from 2 years ago. In spite of this, the app should still work as the specific dependent Python libraries are specified in the requirements.txt
file.
Step 5. Download and unzip contents from the GitHub repo: https://github.com/dataprofessor/eda-app/archive/main.zip
Step 6. Launch the app:
streamlit run app.py
You’ll see the web app browser pop up:
The functionality of this EDA app leverages the capabilities of pandas-profiling. Let's take a look at the app in action:
Congratulations! You now know how to clone a Streamlit app from a GitHub repo, setup a dedicated conda environment, and successfully launch the app!
Before we get into the nuts and bolts of the Streamlit library, let's take a hands-on approach for learning how to use Streamlit. Particularly, creating a simple Hello world app would probably be an expected rite of passage to learning Streamlit!
It's not as difficult as you may think. In fact, it takes only 2 lines of code to do just that!
import streamlit as st
st.write('Hello world!')
Click on the See code explanation toggle button to reveal the explanatory text:
See code explanation
Here's a line-by-line breakdown of the code:
- Import the
streamlit
library asst
(so that we can later refer tostreamlit
literally asst
instead of having to type the full wordstreamlit
. - Use
st.write
to write a text output and inside thest.write
command we use the'Hello world!'
string as the input argument.
In this lesson, we're introduced to Streamlit along with how to setup a computing environment as well as creating our first Streamlit app.