Streamlit: Build and Deploy ML Apps in Pure Python in 10 Minutes

Updated
7 min read
Streamlit: Build and Deploy ML Apps in Pure Python in 10 Minutes
TL;DR

Streamlit turns Python scripts into interactive web apps with no frontend knowledge required - build a working ML demo, RAG chatbot, or data dashboard and deploy it for free.

What Makes Streamlit Different

Streamlit converts a Python script into a web app by running it top-to-bottom every time a user interacts with a widget. No HTML, no JavaScript, no React. Just Python.

This execution model is the key to understanding Streamlit. Every button click, slider move, or text input triggers a full script rerun. This sounds inefficient, but Streamlit optimizes it with caching.

Your First Streamlit App

python
import streamlit as st
import pandas as pd

st.title("Sales Dashboard")

uploaded = st.file_uploader("Upload CSV", type="csv")
if uploaded:
    df = pd.read_csv(uploaded)

    region = st.selectbox("Filter by region", df["region"].unique())
    filtered = df[df["region"] == region]

    st.dataframe(filtered)
    st.bar_chart(filtered.groupby("product")["revenue"].sum())

Run with: streamlit run app.py

Caching: The Essential Optimization

python
import streamlit as st
import pandas as pd
from sentence_transformers import SentenceTransformer

@st.cache_data  # cache data loading  -  reruns only when inputs change
def load_data(path: str) -> pd.DataFrame:
    return pd.read_parquet(path)

@st.cache_resource  # cache heavy objects  -  loaded once, shared across sessions
def load_model() -> SentenceTransformer:
    return SentenceTransformer("all-MiniLM-L6-v2")

df = load_data("embeddings.parquet")  # cached after first run
model = load_model()  # loaded once, reused

Use @st.cache_data for data (serializable, per-user). Use @st.cache_resource for models and database connections (shared across all users).

Building an LLM Chatbot with st.chat_message

python
import streamlit as st
from openai import OpenAI

client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])

if "messages" not in st.session_state:
    st.session_state.messages = []

for msg in st.session_state.messages:
    with st.chat_message(msg["role"]):
        st.write(msg["content"])

if prompt := st.chat_input("Ask anything..."):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.write(prompt)

    with st.chat_message("assistant"):
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=st.session_state.messages,
            stream=True,
        )
        content = st.write_stream(response)

    st.session_state.messages.append({"role": "assistant", "content": content})

Session State for Persistence

python
import streamlit as st

if "counter" not in st.session_state:
    st.session_state.counter = 0

if st.button("Increment"):
    st.session_state.counter += 1

st.write(f"Count: {st.session_state.counter}")

Session state persists across reruns for a single user session.

Secrets Management

toml
# .streamlit/secrets.toml (never commit this)
OPENAI_API_KEY = "sk-..."
DATABASE_URL = "mongodb+srv://..."
python
import streamlit as st
key = st.secrets["OPENAI_API_KEY"]

On Streamlit Community Cloud, secrets are set in the app dashboard.

Deploying for Free

  1. Push your app to GitHub
  2. Go to share.streamlit.io
  3. Connect your repo and set the main file path
  4. Add secrets in the dashboard
  5. Deploy - public URL in seconds

Resources: Streamlit, GitHub, app gallery.

#streamlit#ml-apps#python#deployment#data-visualization

One AI engineering post, weekly

LLM benchmarks, prompt techniques, and token-cost breakdowns, delivered to your inbox.

What should we test or explain next?

Comments

Have a question or something to add? Sign in to join the discussion.

Mahmudul Haque Qudrati — CEO & ML Engineer at Pristren

Mahmudul Haque Qudrati

CEO & ML Engineer

Software architect, full-stack engineer, and machine learning practitioner. Founder & CEO of Pristren, CTO at Venture Kick-backed PIN Sport Application GmbH (Basel, Switzerland), Software Development Lead at DailyFrame GmbH (building Dimetra patient transport software), and creator of Zlyqor. Focused on custom workflow automation, scalable web and mobile applications, and production AI/ML systems.