Streamlit is an open-source Python library that makes it incredibly easy to create beautiful, custom web applications for machine learning and data science. With Streamlit, you can turn data scripts into shareable web apps in minutes, all in pure Python, without needing any front-end development experience (HTML, CSS, JavaScript).
It's designed for data scientists and ML engineers who want to quickly showcase their work, build interactive dashboards, or create prototypes of AI applications. Streamlit handles all the complexities of web development, allowing you to focus on your data and models.
Streamlit offers several compelling advantages for AI and ML projects:
Getting started with Streamlit is straightforward:
First, ensure you have a Python environment activated. Then, install Streamlit using `pip` or `uv`:
Installation with pip:
pip install streamlitInstallation with uv:
uv pip install streamlitmy_app.py)Create a file named `my_app.py` and add the following code:
import streamlit as st import pandas as pd import numpy as np st.set_page_config(layout="wide") st.title("My First Streamlit AI App") st.write("Welcome to an interactive demonstration!") # Add a slider age = st.slider("Select your age", 0, 100, 25) st.write("You selected: " + str(age) + " years old") # Add a button if st.button("Say Hello"): st.write("Hello, Streamlit user!") # Display a DataFrame st.header("Sample Data") data = pd.DataFrame(np.random.rand(10, 3), columns=["col1", "col2", "col3"]) st.dataframe(data) # Plot a chart st.header("Sample Chart") chart_data = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"]) st.line_chart(chart_data) st.sidebar.header("About This App") st.sidebar.write("This is a simple Streamlit app to demonstrate basic features.") st.info("App loaded successfully!")Open your terminal or command prompt, navigate to the directory where you saved `my_app.py`, and run:
streamlit run my_app.pyThis command will open your Streamlit app in your default web browser. Any changes you save to `my_app.py` will automatically update the app.
st.write()Description: The magic command! It can write almost anything to your app: text, data, Matplotlib figures, Plotly charts, and more.
Usage:
st.write("Hello, world!") st.write(my_dataframe)st.slider(), st.text_input(), st.button())Description: Allow users to interact with your app. When a widget's value changes, Streamlit reruns your script from top to bottom.
Usage:
name = st.text_input("Enter your name") if st.button("Submit"): st.write("Hello, " + name + "!")st.dataframe(), st.table())Description: Specialized functions for displaying Pandas DataFrames and static tables efficiently.
Usage:
df = pd.DataFrame(np.random.rand(2, 2), columns=["A", "B"]) st.dataframe(df)st.line_chart(), st.bar_chart(), st.pyplot())Description: Functions for quickly rendering various types of charts directly from data or by integrating with other plotting libraries.
Usage:
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3]) st.pyplot(fig)st.sidebar, st.columns(), st.expander())Description: Tools to organize your app's layout, allowing for sidebars, multiple columns, and collapsible sections.
Usage:
col1, col2 = st.columns(2) with col1: st.write("Content for column 1") with col2: st.write("Content for column 2") with st.expander("Click to expand"): st.write("Hidden content here")@st.cache_data, @st.cache_resource)Description: Decorators to optimize app performance by caching expensive computations (like loading data or training models) so they only run once.
Usage:
@st.cache_data def load_data(): # Load data from a heavy source return pd.DataFrame(...) @st.cache_resource def train_model(): # Train a machine learning model return model data = load_data() model = train_model()While Streamlit handles much of the UI, following these practices can significantly enhance user experience: