In this lesson, we'll get you started with Streamlit from going over the overarching concept of this low-code web framework, setting up a coding environment and to writing your first Streamlit app.
- What is Streamlit?
- Prerequisites
- Installing Streamlit
- Setting up your Streamlit workspace
- Creating your first Streamlit app
- Running the Streamlit app
- Methods for displaying information in the app
- Input widgets for accepting user information in the app
- Chat elements for building a chatbot
- Summary
- Examples
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.8
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/ydata_profiling/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
ydata_profiling
streamlit_pandas_profiling
Step 5. Download and unzip contents from the GitHub repo: https://github.com/dataprofessor/ydata_profiling/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.
Locally, you can run the newly created Streamlit app by launching a command-line terminal and enter the following:
streamlit run streamlit_app.py
where streamlit_app.py
is the Streamlit app that you've just created.
Now that we know how to create your first Streamlit app and get it up and running, it's now time to explore how we can display information in the app.
In the Streamlit Documentation page, the Text elements and Write and magic pages provide several ways in which information can be displayed.
Here's a list of methods for displaying information in app:
st.title()
- Display the app's title.st.header()
- Display text as a section headerst.subheader()
- Display text as a sub-section headerst.write()
- Can both display text and write arguments to the app.st.markdown()
- Display text in Markdown formatst.text()
- Display fixed width and pre-formatted textst.code()
- Display code that can be copiedst.caption()
- Display small caption textst.latex()
- Display LaTeX expressions
Proceed to Project 0 to see how to use these widgets in a Streamlit app.
A great part about building data-driven apps is the ability to take in user input via various widgets, for example, sliders, text input, number input, color selector, etc. Such widget input can then be used to set model parameters, assign values to function parameters, and so much more.
Here's a list of common input widgets that I typically use:
st.text_input()
- Displays a single-line text input widgetst.number_input()
- Displays a number input widgetst.selectbox()
- Displays a drop-down selection widgetst.multiselect()
- Displays a multi-selection widgetst.slider()
- Displays either a single-value slider or a range sliderst.file_uploader()
- Displays a file upload widgetst.button()
- Displays a button widgetst.download_button()
- Displays a download button widget
Note: It should be mentioned that aside from those listed above, there are several more input widgets from which you can use. More information on the Input widgets Docs page.
Proceed to Project 0 to see how to use these widgets in a Streamlit app.
Streamlit currently provides 3 chat elements widgets (more info in the Chat elements Docs page) that is designed for you to use in conjunction with one another, for example, in building a chatbot (or you can also use them separately).
Here's a list of the chat elements:
st.chat_input()
- Displays a chat input widgetst.chat_message()
- Inserts a chat message container for displaying LLM generated responsesst.status()
- Inserts a status container for display output from long-running tasks
Proceed to Project 0 to see how to use these widgets in a Streamlit app.
In this lesson, we're introduced to Streamlit along with how to setup a computing environment as well as creating our first Streamlit app.