Streamlit
From Python Script to Web App in Minutes
Streamlit lets you turn any Python script into a beautiful, interactive web application with zero frontend knowledge. It is the fastest way to build and share AI/ML demos.
What is Streamlit?
The Fastest Way to Build Data & AI Apps
Simple Definition:
Streamlit is an open-source Python framework that turns your data scripts into shareable web apps. You write pure Python -- no HTML, no CSS, no JavaScript needed. Every time you save your script, the app refreshes automatically.
It was created by former Google engineers in 2019 and acquired by Snowflake in 2022. Today it is the #1 choice for ML engineers to prototype and demo AI models.
Analogy - Maggi Noodles of Web Development:
Think of Streamlit as Maggi noodles. You want a quick snack (demo app)? You do not need to be a chef (frontend developer). Just boil water (write Python), add masala (Streamlit widgets), and in 2 minutes you have something delicious (working web app). It is not a 5-star restaurant meal (production frontend), but it gets the job done fast!
Why Streamlit Became So Popular:
- Zero Frontend Skills: Write only Python, Streamlit handles UI rendering
- Live Reload: App updates instantly when you save the script
- Built-in Widgets: Sliders, buttons, file uploaders, charts -- all one-liners
- Free Hosting: Streamlit Community Cloud deploys from GitHub for free
- Huge Ecosystem: 500+ community components for maps, auth, dashboards
Note: Streamlit is used by teams at Google, Amazon, and hundreds of Indian startups for internal tools and ML demos. If you know Python, you can build a web app today.
Core Architecture - How Streamlit Works
The Re-run Model That Makes Everything Simple
The Magic Behind Streamlit:
Streamlit uses a unique execution model: every time a user interacts with the app (clicks a button, moves a slider), the entire Python script re-runs from top to bottom. This sounds inefficient, but it is what makes Streamlit so simple -- no callbacks, no event handlers, no state management boilerplate.
Architecture Breakdown:
- Frontend (React): Streamlit renders your Python-defined widgets as React components in the browser. You never touch React directly.
- Backend (Tornado Server): A Python server runs your script and communicates with the frontend via WebSocket.
- Protobuf Protocol: Widget values are serialized using Protocol Buffers for fast communication between frontend and backend.
- Session State: Each user gets their own session. Variables persist across re-runs using st.session_state -- like a Python dictionary that survives re-runs.
The Re-run Gotcha:
Since the whole script re-runs, expensive operations (loading ML models, API calls) would repeat every time. Streamlit solves this with st.cache_data and st.cache_resource:
- st.cache_data: Caches return values of functions. Great for dataframes, API responses.
- st.cache_resource: Caches global resources like ML models, database connections. Shared across all users.
Think of caching as "yeh pehle se bana hua hai, dubara banana nahi hai" -- it remembers previous results.
Note: The re-run model is Streamlit's biggest strength AND its biggest limitation. Simple for beginners, but can get tricky for complex multi-page apps with lots of state.
Key Widgets and Layout System
Building Blocks of Every Streamlit App
Essential Widget Categories:
| Category | Widgets | Use Case |
|---|---|---|
| Input | text_input, text_area, number_input | User prompts, parameters |
| Selection | selectbox, multiselect, slider, radio | Model selection, hyperparameters |
| Media | file_uploader, camera_input, audio | Upload images, record audio |
| Display | write, markdown, dataframe, metric | Show results, data tables |
| Charts | line_chart, bar_chart, plotly_chart | Visualize model outputs |
Layout Options:
- st.columns(): Side-by-side layout like newspaper columns
- st.sidebar: Left sidebar for settings and filters (like IRCTC sidebar filters)
- st.tabs(): Tab-based navigation for different views
- st.expander(): Collapsible sections for optional details
- st.container(): Group elements logically
AI-Specific Features:
- st.chat_message / st.chat_input: Build ChatGPT-like interfaces with just 2 lines
- st.write_stream(): Stream LLM responses token-by-token (typewriter effect)
- st.status(): Show agent thinking steps with expandable details
- st.progress(): Progress bars for model training or data processing
Note: Streamlit has 50+ built-in widgets. For AI apps, the chat components (st.chat_message, st.chat_input) added in 2023 are game changers.
Building an AI Chatbot with Streamlit
Practical Architecture for LLM-Powered Apps
Typical AI App Architecture with Streamlit:
- Step 1 - Initialize: Load API keys from environment variables or st.secrets. Initialize session state for chat history.
- Step 2 - Sidebar: Model selector (GPT-4, Claude, Gemini), temperature slider, system prompt input.
- Step 3 - Chat History: Loop through st.session_state.messages and display using st.chat_message for each role (user/assistant).
- Step 4 - User Input: st.chat_input captures the user message, appends to history.
- Step 5 - LLM Call: Send conversation to API, stream response using st.write_stream.
- Step 6 - Persist: Append assistant response to session state.
Real-World Indian Use Cases:
- Resume Screener: Upload resumes (PDF), AI scores and ranks candidates for Naukri-style platforms
- Crop Disease Detector: Farmer uploads leaf photo, model identifies disease and suggests treatment
- Legal Document Analyzer: Upload FIR or court order, AI summarizes in simple Hindi
- Stock Analyzer: Enter NSE stock symbol, get AI-powered analysis with charts
Deployment Options:
- Streamlit Community Cloud: Free, direct from GitHub, best for demos
- Docker + Fly.io / Railway: More control, better for internal tools
- AWS/GCP/Azure: Enterprise deployments with scaling
- Hugging Face Spaces: Free GPU for ML-heavy apps
Note: Streamlit is excellent for prototyping and internal tools. For customer-facing production apps with thousands of users, consider Next.js or a proper frontend framework.
Streamlit Limitations and When NOT to Use It
Know the Boundaries Before You Build
Key Limitations:
- Re-run Model: Entire script re-runs on every interaction. Complex apps with many widgets become slow. Not suitable for apps needing partial page updates.
- No Real Authentication: Built-in auth is basic. For proper login/signup with roles, you need custom solutions or third-party packages.
- Limited Customization: You are stuck with Streamlit's styling. Custom CSS is hacky and can break across versions.
- Scaling Issues: Each user session runs a separate Python process. 100 concurrent users = 100 Python processes = high memory.
- No SEO: Single-page app with no server-side rendering. Google cannot crawl it. Not for public-facing content sites.
When to Use vs Not Use:
| Use Streamlit | Do NOT Use Streamlit |
|---|---|
| ML model demos and prototypes | Customer-facing SaaS products |
| Internal data dashboards | E-commerce or transactional apps |
| Quick AI chatbot POCs | High-traffic public websites |
| Data exploration tools | Mobile-first applications |
| Hackathon projects | Apps needing complex routing |
Note: Streamlit is a prototyping tool, not a production frontend framework. Use it to validate ideas fast, then migrate to proper tech if the idea succeeds.
Interview Questions - Streamlit
Q: How does Streamlit execution model work? Why does the entire script re-run?
Streamlit follows a top-to-bottom re-run model. Every user interaction (click, slider move, input change) triggers a full script re-execution. This eliminates the need for callbacks or event handlers, making the code linear and simple. Caching (st.cache_data, st.cache_resource) prevents expensive operations from repeating. Session state preserves values across re-runs.
Q: What is the difference between st.cache_data and st.cache_resource?
st.cache_data caches serializable return values (DataFrames, strings, lists). Each user gets their own copy. st.cache_resource caches non-serializable global resources (ML models, DB connections) shared across all users. Use cache_data for data, cache_resource for connections and models.
Q: How would you build a streaming chatbot with Streamlit?
Use st.chat_input for user messages, st.chat_message to display conversation bubbles, st.session_state.messages to store history, and st.write_stream to stream LLM responses token-by-token. The session state persists chat history across re-runs so the full conversation is always visible.
Q: What are the scaling limitations of Streamlit?
Each user session spawns a separate Python process with its own memory. 100 concurrent users means 100 processes. The re-run model adds CPU overhead. There is no built-in CDN, load balancing, or horizontal scaling. For high traffic, you need Docker containers behind a load balancer, but even then Streamlit is not designed for thousands of concurrent users.
Q: When would you choose Streamlit over Gradio for an ML demo?
Choose Streamlit when you need custom layouts, multi-page apps, dashboards, or complex data visualization. Choose Gradio when you want a quick model interface with input-output mapping (image in, label out). Streamlit gives more control; Gradio is faster for simple model demos. Gradio also integrates better with Hugging Face ecosystem.
Frequently Asked Questions
What is Streamlit?
Streamlit lets you turn any Python script into a beautiful, interactive web application with zero frontend knowledge. It is the fastest way to build and share AI/ML demos.
How does Streamlit work?
The Fastest Way to Build Data & AI Apps Simple Definition: Streamlit is an open-source Python framework that turns your data scripts into shareable web apps. You write pure Python -- no HTML, no CSS, no JavaScript needed.
Related topics
Practice this on DevInterviewMaster
Read the full Streamlit breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.