0
# Streamlit
1
2
A powerful Python framework that transforms Python scripts into interactive web applications in minutes. Streamlit enables developers and data scientists to create dashboards, reports, and chat applications without requiring web development expertise.
3
4
## Package Information
5
6
- **Package Name**: streamlit
7
- **Language**: Python
8
- **Installation**: `pip install streamlit`
9
- **CLI Command**: `streamlit run app.py`
10
11
## Core Imports
12
13
```python
14
import streamlit as st
15
```
16
17
## Basic Usage
18
19
```python
20
import streamlit as st
21
import pandas as pd
22
import numpy as np
23
24
# Set page configuration
25
st.set_page_config(
26
page_title="My App",
27
page_icon="🎯",
28
layout="wide"
29
)
30
31
# Display title and text
32
st.title("My Data Dashboard")
33
st.markdown("Welcome to my **Streamlit** app!")
34
35
# Create some sample data
36
data = pd.DataFrame({
37
'x': np.random.randn(100),
38
'y': np.random.randn(100)
39
})
40
41
# Display interactive widgets
42
name = st.text_input("Enter your name:")
43
age = st.slider("Select your age:", 0, 100, 25)
44
show_data = st.checkbox("Show raw data")
45
46
# Display charts and data
47
st.line_chart(data)
48
49
if show_data:
50
st.dataframe(data)
51
52
# Display metrics
53
col1, col2 = st.columns(2)
54
with col1:
55
st.metric("Mean X", f"{data['x'].mean():.2f}")
56
with col2:
57
st.metric("Mean Y", f"{data['y'].mean():.2f}")
58
59
# Create form for user input
60
with st.form("feedback_form"):
61
feedback = st.text_area("Provide feedback:")
62
rating = st.selectbox("Rating:", [1, 2, 3, 4, 5])
63
submitted = st.form_submit_button("Submit")
64
65
if submitted:
66
st.success(f"Thank you {name}! Rating: {rating}/5")
67
```
68
69
## Architecture
70
71
Streamlit follows a reactive programming model built on several key concepts:
72
73
- **Script Re-execution**: The entire script re-runs when user interactions change widget values
74
- **Session State**: Persistent state management across script re-runs via `st.session_state`
75
- **Caching**: Performance optimization through `@st.cache_data` and `@st.cache_resource` decorators
76
- **Delta Generator**: Core abstraction that manages the display tree and updates efficiently
77
- **Widget State**: Automatic state tracking for all input widgets with optional callbacks
78
79
This design enables rapid development by treating web apps as simple Python scripts while providing powerful interactive capabilities through automatic state management and efficient UI updates.
80
81
## Capabilities
82
83
### Display Elements
84
85
Core functions for displaying text, data, charts, and media content. Includes everything from simple text and markdown to interactive dataframes and complex visualizations.
86
87
```python { .api }
88
def title(body, anchor=None, *, help=None): ...
89
def header(body, anchor=None, *, help=None, divider=False): ...
90
def markdown(body, *, unsafe_allow_html=False, help=None): ...
91
def dataframe(data, width=None, height=None, *, use_container_width=False, hide_index=None, column_order=None, column_config=None): ...
92
def line_chart(data, *, x=None, y=None, color=None, width=None, height=None, use_container_width=False, help=None): ...
93
def image(image, caption=None, width=None, *, use_column_width=None, clamp=False, channels="RGB", output_format="auto", help=None): ...
94
```
95
96
[Display Elements](./display-elements.md)
97
98
### Input Widgets
99
100
Interactive widgets for user input including buttons, text inputs, selection widgets, file uploads, and form controls.
101
102
```python { .api }
103
def button(label, key=None, help=None, on_click=None, args=None, kwargs=None, *, type="secondary", disabled=False, use_container_width=False, icon=None): ...
104
def text_input(label, value="", max_chars=None, key=None, type="default", help=None, autocomplete=None, on_change=None, args=None, kwargs=None, *, placeholder=None, disabled=False, label_visibility="visible"): ...
105
def selectbox(label, options, index=0, format_func=str, key=None, help=None, on_change=None, args=None, kwargs=None, *, placeholder="Choose an option", disabled=False, label_visibility="visible"): ...
106
def slider(label, min_value=None, max_value=None, value=None, step=None, format=None, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible"): ...
107
def file_uploader(label, type=None, accept_multiple_files=False, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False, label_visibility="visible"): ...
108
```
109
110
[Input Widgets](./input-widgets.md)
111
112
### Layout and Containers
113
114
Functions for organizing app layout including columns, containers, sidebars, tabs, and expandable sections.
115
116
```python { .api }
117
def columns(spec, *, gap="small"): ...
118
def container(*, height=None, border=None): ...
119
def expander(label, *, expanded=False, icon=None): ...
120
def tabs(tab_list): ...
121
def form(key, *, clear_on_submit=False, border=True): ...
122
```
123
124
[Layout and Containers](./layout-containers.md)
125
126
### State Management
127
128
Session state, query parameters, and context management for maintaining application state across interactions.
129
130
```python { .api }
131
session_state: SessionStateProxy
132
query_params: QueryParamsProxy
133
context: ContextProxy
134
```
135
136
[State Management](./state-management.md)
137
138
### Caching and Performance
139
140
Caching decorators and performance optimization tools for efficient data processing and resource management.
141
142
```python { .api }
143
def cache_data(func=None, *, ttl=None, max_entries=None, show_spinner=True, persist=None, experimental_allow_widgets=False, hash_funcs=None, validate=None): ...
144
def cache_resource(func=None, *, ttl=None, max_entries=None, show_spinner=True, validate=None, hash_funcs=None): ...
145
```
146
147
[Caching and Performance](./caching-performance.md)
148
149
### Navigation and Pages
150
151
Multi-page application support, navigation controls, and execution flow management.
152
153
```python { .api }
154
def set_page_config(page_title=None, page_icon=None, layout="centered", initial_sidebar_state="auto", menu_items=None): ...
155
def navigation(pages, *, position="sidebar", expanded=True): ...
156
class Page: ...
157
def switch_page(page): ...
158
def rerun(): ...
159
def stop(): ...
160
```
161
162
[Navigation and Pages](./navigation-pages.md)
163
164
### User and Authentication
165
166
User authentication, login/logout functionality, and user information access.
167
168
```python { .api }
169
user: UserInfoProxy
170
def login(provider, *, oauth2=None): ...
171
def logout(): ...
172
```
173
174
[User and Authentication](./user-auth.md)
175
176
### Advanced Features
177
178
Chat interfaces, app fragments, modal dialogs, and database connections for sophisticated applications.
179
180
```python { .api }
181
def chat_message(name, *, avatar=None): ...
182
def chat_input(placeholder=None, key=None, max_chars=None, on_submit=None, args=None, kwargs=None, *, disabled=False): ...
183
def fragment(func): ...
184
def dialog(title, *, width="small"): ...
185
def connection(name, type=None, **kwargs): ...
186
```
187
188
[Advanced Features](./advanced-features.md)
189
190
### Components and Configuration
191
192
Custom component framework and column configuration for data editors and dataframes.
193
194
```python { .api }
195
# Custom components
196
components.v1.html(html, *, width=None, height=None, scrolling=False): ...
197
components.v1.declare_component(name, path=None, url=None): ...
198
199
# Column configuration
200
column_config.TextColumn(label=None, width=None, help=None, disabled=None, required=None, default=None, max_chars=None, validate=None): ...
201
column_config.NumberColumn(label=None, width=None, help=None, disabled=None, required=None, default=None, min_value=None, max_value=None, step=None, format=None): ...
202
```
203
204
[Components and Configuration](./components-config.md)
205
206
## Global Objects and Utilities
207
208
```python { .api }
209
# Sidebar access
210
sidebar: DeltaGenerator
211
212
# Secrets management
213
secrets: SecretsProxy
214
215
# Version information
216
__version__: str
217
218
# Configuration
219
def get_option(key): ...
220
def set_option(key, value): ...
221
222
# Utility functions
223
def help(obj): ...
224
def write(*args, unsafe_allow_html=False): ...
225
def write_stream(stream, *, help=None): ...
226
def echo(code_location="above"): ...
227
```