Streamlit

On this page 7

Streamlit is an open-source Python framework that turns an ordinary data or machine learning script into an interactive web application without writing HTML, CSS or JavaScript. Among AI frameworks & tools, it occupies the presentation layer: it does not train or serve models itself, but it gives model builders a fast way to put a model, a dataset or a language model pipeline in front of colleagues, stakeholders and test users.

The framework was released in 2019 and acquired by Snowflake in 2022, and it remains free under the Apache 2.0 license. Its appeal in artificial intelligence work comes from one design decision: the whole script reruns from top to bottom whenever a user interacts with a widget, which lets a data scientist think in plain sequential code instead of callbacks and event handlers.

How the Streamlit rerun model works

Streamlit works by executing the entire Python script on every interaction and sending the resulting element tree to the browser. A command such as st.slider both draws a widget and returns its current value, so the next line of code can use that value directly, exactly as in a notebook cell.

Behind the scenes, a Tornado-based server keeps a WebSocket connection to each browser tab, and every tab gets its own session. When a widget changes, the browser sends the new value, the server reruns the script for that session only, and a React front end applies the difference between the old and new element trees. The developer never sees this protocol.

The table below lists the core building blocks that make the rerun model practical.

MechanismTypical commandWhat it solves
Widgetsst.slider, st.text_inputCollect input and return values inline
Data cachingst.cache_dataAvoid reloading or recomputing data on each rerun
Resource cachingst.cache_resourceKeep one shared model or connection in memory
Session statest.session_statePersist values such as chat history between reruns
Fragmentsst.fragmentRerun only part of the page instead of the full script
Formsst.formBatch several inputs into one submission
Core Streamlit mechanisms and their purpose

Streamlit features for AI applications

Streamlit has become a default prototyping surface for language model applications because it ships chat-specific elements. st.chat_message renders a message bubble, st.chat_input places a prompt box at the bottom of the page, and st.write_stream displays tokens as they arrive from a streaming model API.

Combined with session state for conversation history, these elements let a developer build a working chatbot over LangChain, LlamaIndex or a direct model API in a few dozen lines. For classical machine learning, the same pattern applies: a model trained with scikit-learn, PyTorch or XGBoost is loaded once with resource caching, and widgets feed it new inputs for live predictions.

  • Data display: st.dataframe and st.data_editor show and edit tables interactively.
  • Charts: built-in charts plus support for Altair, Plotly, Matplotlib and other libraries.
  • Media: image, audio, video and camera input for computer vision and speech demos.
  • Multipage apps: st.navigation and a pages folder organize larger tools.
  • Custom components: React or plain HTML extensions when built-in widgets are not enough.

Streamlit compared with Gradio and full web frameworks

Streamlit differs from its closest neighbor, Gradio, mainly in its programming model: Gradio is built around explicit input-output functions and event listeners, while Streamlit relies on top-to-bottom reruns. Compared with general web frameworks, both trade flexibility for speed of development.

The comparison below summarizes where each option fits when a team needs a user interface for a model.

ToolProgramming modelBest fitMain limitation
StreamlitScript reruns on each interactionDashboards, internal tools, multi-step data appsRerun cost and limited layout control
GradioFunction with typed inputs and outputsSingle-model demos and Hugging Face SpacesLess natural for dashboard-style pages
DashCallbacks wired to component propertiesComplex analytical dashboardsMore boilerplate code
Flask or FastAPI with a front endSeparate API and client codeProduction products with custom designRequires front-end skills and more time
Jupyter notebookCells run manuallyExploration by the authorNot a usable interface for other people
Streamlit versus related interface options

A useful rule is that Streamlit wins when the audience is internal and the goal is insight or validation, while a dedicated web stack wins when the interface itself is the product customers pay for.

Deploying and scaling Streamlit apps

A Streamlit app is deployed as a long-running Python server process, started with streamlit run app.py, and it listens on port 8501 by default. Because each user session holds a WebSocket and its own state on the server, the process is stateful, which shapes how it scales.

The deployment options below differ mostly in who manages the server and how access is controlled.

TargetTypical usePoints to watch
Streamlit Community CloudFree hosting of public or small apps from GitHubResource limits and apps that sleep when idle
Snowflake integrationApps running next to warehouse dataPlatform-specific package availability
Hugging Face SpacesPublic model demosShared hardware unless upgraded
Docker on a cloud platformInternal tools with company loginSticky sessions needed behind load balancers
KubernetesMany users or many appsWebSocket support and memory per session
Common deployment targets for Streamlit

Horizontal scaling requires sticky sessions, because a session cannot move between replicas mid-conversation. Memory planning matters as well: a cached model is shared across sessions, but anything stored in session state is duplicated per user.

Limitations and common mistakes with Streamlit

The rerun model that makes Streamlit simple is also the source of most performance problems. An expensive step without caching runs again on every click, and a page with many widgets can feel slow once the script takes more than a second or two to complete.

The table below pairs frequent symptoms with their usual cause and fix.

SymptomLikely causeFix
Model reloads on every clickLoading code is not cachedWrap the loader in st.cache_resource
Chat history disappearsMessages kept in local variablesStore them in st.session_state
Stale data after updatesCache never expiresSet a ttl or clear the cache deliberately
Users see each other’s dataMutable object returned from a shared cacheCopy results or use st.cache_data correctly
Page flickers while typingEach keystroke triggers a rerunGroup inputs inside st.form
Typical Streamlit problems and fixes

Beyond performance, Streamlit offers limited control over layout and styling, and it has no built-in user management beyond recent authentication helpers, so production access control usually comes from a reverse proxy or platform login. Teams should also remember that secrets belong in `secrets.toml` or environment variables, never in the script itself.

In simple terms

Streamlit lets a person who writes Python turn their code into a small website with buttons, sliders and charts. They do not need to learn web design. They write the script as usual, add a few lines that create input boxes, and Streamlit builds the page.

Each time someone moves a slider or types a question, Streamlit simply runs the script again with the new input and redraws the page. It is like a calculator that recalculates everything whenever any number changes. That makes it ideal for quick demos and internal tools, though less suited to large public websites.

A practical example

A data science team at a regional insurance company has trained a gradient-boosted model that estimates claim fraud risk. Claims adjusters need to try it on real cases before anyone commits to integrating it into the core claims system, which would take months.

One data scientist builds a Streamlit app in about two days. The model loads once through st.cache_resource, a form collects claim amount, policy age and incident type, and the page shows the risk score with a chart of the main contributing features. A second page adds a chat assistant, built with st.chat_input, that answers questions about the claims manual using a retrieval pipeline.

The app runs in a Docker container behind the company login and serves about 40 adjusters. Within three weeks their feedback reveals that the model overrates policy age, the team retrains it, and the validated model moves to production with clear requirements instead of guesses.