By the end of this lesson, you will be able to:
:information_source: Definition: Streamlit is an open-source app framework that lets you create web applications using Python. It's especially popular for Machine Learning and Data Science projects.
Streamlit helps you turn your Python code into interactive web apps quickly and easily. You don't need to know HTML, CSS, or JavaScript - just Python!
The company started in San Francisco, California in 2018. Three friends - Adrien Treuille, Amanda Kelly, and Thiago Teixeira - created it to make building data apps easier.
The platform gives you tools to:
:memo: Streamlit works with many popular Python libraries you might already know:
- Scikit Learn - for machine learning
- OpenCV - for computer vision
- TensorFlow and PyTorch - for deep learning
- NumPy and Pandas - for data manipulation
- Matplotlib, Plotly, and Seaborn - for creating charts
- And many more!
To learn more about Streamlit, you can visit Streamlit Documentation here: https://docs.streamlit.io/
Before building your first web app, you need to set up Streamlit on your computer. Let's do this in two steps:
First, let's get Streamlit on your computer:
Open VSCode - We'll use VSCode to write our code. (Need help setting up VSCode? Watch this setup video)
Install Streamlit - Open the terminal in VSCode and type:
py -m pip install streamlit
Import Streamlit in your Python file:
import streamlit as st
Test your installation - Run this command to see a demo app:
streamlit hello
:bulb: The
as st
part creates a shortcut. Instead of typingstreamlit.title()
, you can just typest.title()
!
Step 2: Run Your Streamlit App
To run your own Streamlit app:
- Save your Python file (for example:
my_app.py
)- In the terminal, type:
bashpy -m streamlit run my_app.py
- Your web browser will open automatically with your app!
:art: Building a Streamlit Web Interface
Now for the fun part! Let's learn how to add different elements to your web app. Think of these as building blocks for your website.
:memo: Display Text Elements
Streamlit gives you many ways to show text on your webpage. Here are the most useful ones:
One. Title - The biggest text, perfect for your app's name
python
st.title("My Amazing App")
2. Header - Large text for section titles
python
st.header("Welcome Section")
3. Subheader - Medium text for subsections
python
st.subheader("About This App")
4. Regular Text - Simple text display
python
st.text("This is plain text")
5. Markdown - Text with formatting (bold, italic, links)
python
st.markdown("This is **bold** and this is *italic*")
note Markdown lets you format text easily! Use
**text**
for bold and*text*
for italic.
Make your app more visual by adding images and videos!
Display an Image:
st.image(image, width=300, caption="My cool image")
image
- Your image file or URLwidth
- How wide the image should be (in pixels)caption
- Text under the imageDisplay a Video:
st.video(video_file)
video_file
- Your video file or URLWidgets make your app interactive! Users can click buttons and select options.
Button - Creates a clickable button
if st.button("Click Me!"):
st.write("You clicked the button!")
Select Box - Creates a dropdown menu
choice = st.selectbox("What's your favorite color?", ("Red", "Blue", "Green"))
Sidebar - Adds a side panel to organize controls
st.sidebar.title("Settings")
st.sidebar.button("Save")
:bulb: Want to explore more? Streamlit can also display:
- :bar_chart: Data frames and tables
- :chart_with_upwards_trend: Charts and graphs
- :emoji: Audio files
Check out the full list at: https://docs.streamlit.io/library/api-reference
Your app becomes truly interactive when users can input information! Let's learn how to collect different types of input.
Single-line Text Input - Perfect for short answers like names
name = st.text_input("What is your name?")
st.write(f"Hello, {name}!")
Multi-line Text Area - Great for longer text like addresses or comments
address = st.text_area("What is your address?")
st.write(f"You live at: {address}")
File Upload - Let users upload documents, images, or data files
file_upload = st.file_uploader("Upload a file")
if file_upload is not None:
st.write("File uploaded successfully!")
Camera Input - Access the user's webcam for photos
webcam_input = st.camera_input("Take a picture")
if webcam_input is not None:
st.image(webcam_input)
:memo: Note Always save user input to a variable so you can use it later in your program!
You've learned the basics of Streamlit! Here's what you can do now:
Remember: Streamlit turns your Python code into web apps without needing to learn web development!
Code with AI: Try using AI to create Streamlit elements.
Prompts to try:
Hello World App - Create your first Streamlit app that displays "Hello World" with a title and header.
Personal Info Form - Build an app that:
Photo Gallery - Create an app that:
Interactive Story - Build an app where:
Remember to run your app with py -m streamlit run filename.py
to see your creation!