pypi-streamlit

Description
The fastest way to build and share data apps
Author
tessl
Last updated

How to use

npx @tessl/cli registry install tessl/pypi-streamlit@1.16.0

display-elements.md docs/

1
# Display Elements and Text
2
3
Core functions for displaying text, data, and status messages. These elements form the foundation of Streamlit's content display capabilities, providing everything from basic text formatting to sophisticated data visualization.
4
5
## Capabilities
6
7
### Text and Headers
8
9
Display formatted text content with various emphasis levels and styling options.
10
11
```python { .api }
12
def title(body: str, anchor: str = None) -> DeltaGenerator:
13
"""
14
Display text in title formatting as the largest header.
15
16
Parameters:
17
- body: The text to display as title
18
- anchor: Optional anchor name for the title
19
"""
20
21
def header(body: str, anchor: str = None) -> DeltaGenerator:
22
"""
23
Display text in header formatting (H1 level).
24
25
Parameters:
26
- body: The text to display as header
27
- anchor: Optional anchor name for the header
28
"""
29
30
def subheader(body: str, anchor: str = None) -> DeltaGenerator:
31
"""
32
Display text in subheader formatting (H2 level).
33
34
Parameters:
35
- body: The text to display as subheader
36
- anchor: Optional anchor name for the subheader
37
"""
38
39
def text(body: str) -> DeltaGenerator:
40
"""
41
Display plain text.
42
43
Parameters:
44
- body: The text to display
45
"""
46
47
def caption(body: str, unsafe_allow_html: bool = False) -> DeltaGenerator:
48
"""
49
Display text in small, muted formatting for captions.
50
51
Parameters:
52
- body: The text to display as caption
53
- unsafe_allow_html: Whether to allow HTML tags (use with caution)
54
"""
55
```
56
57
#### Usage Example
58
59
```python
60
import streamlit as st
61
62
st.title("My Application")
63
st.header("Data Analysis")
64
st.subheader("Results Summary")
65
st.text("This is regular text content.")
66
st.caption("This is a caption with smaller, muted text.")
67
```
68
69
### Markdown and Code
70
71
Display rich formatted content including Markdown, LaTeX, and syntax-highlighted code.
72
73
```python { .api }
74
def markdown(body: str, unsafe_allow_html: bool = False) -> DeltaGenerator:
75
"""
76
Display Markdown-formatted text.
77
78
Parameters:
79
- body: The Markdown text to display
80
- unsafe_allow_html: Whether to allow HTML tags (use with caution)
81
"""
82
83
def latex(body: str) -> DeltaGenerator:
84
"""
85
Display LaTeX-formatted mathematical expressions.
86
87
Parameters:
88
- body: The LaTeX expression to render
89
"""
90
91
def code(body: str, language: str = "python") -> DeltaGenerator:
92
"""
93
Display code with syntax highlighting.
94
95
Parameters:
96
- body: The code string to display
97
- language: Programming language for syntax highlighting
98
"""
99
```
100
101
#### Usage Example
102
103
```python
104
import streamlit as st
105
106
# Markdown content
107
st.markdown("""
108
## My Report
109
This is **bold** and this is *italic*.
110
""")
111
112
# LaTeX expression
113
st.latex(r"\sum_{i=1}^{n} x_i^2")
114
115
# Code with syntax highlighting
116
st.code("""
117
def hello_world():
118
print("Hello, Streamlit!")
119
""", language="python")
120
```
121
122
### Data Display
123
124
Present structured data in tables and interactive dataframes.
125
126
```python { .api }
127
def dataframe(data, width: int = None, height: int = None, use_container_width: bool = False) -> DeltaGenerator:
128
"""
129
Display an interactive dataframe.
130
131
Parameters:
132
- data: pandas.DataFrame, dict, list, or array-like data
133
- width: Width in pixels (None for auto-sizing)
134
- height: Height in pixels (None for auto-sizing)
135
- use_container_width: Whether to use full container width
136
"""
137
138
def table(data) -> DeltaGenerator:
139
"""
140
Display a static table.
141
142
Parameters:
143
- data: pandas.DataFrame, dict, list, or array-like data
144
"""
145
146
def json(body) -> DeltaGenerator:
147
"""
148
Display JSON data in an expandable tree format.
149
150
Parameters:
151
- body: JSON-serializable object to display
152
"""
153
```
154
155
#### Usage Example
156
157
```python
158
import streamlit as st
159
import pandas as pd
160
161
# Sample data
162
data = pd.DataFrame({
163
'Name': ['Alice', 'Bob', 'Charlie'],
164
'Age': [25, 30, 35],
165
'City': ['New York', 'London', 'Tokyo']
166
})
167
168
# Interactive dataframe
169
st.dataframe(data)
170
171
# Static table
172
st.table(data)
173
174
# JSON display
175
st.json({
176
"users": ["alice", "bob"],
177
"settings": {"theme": "dark", "notifications": True}
178
})
179
```
180
181
### Metrics and Key Values
182
183
Display key performance indicators and metrics with optional delta indicators.
184
185
```python { .api }
186
def metric(label: str, value, delta=None, delta_color: str = "normal") -> DeltaGenerator:
187
"""
188
Display a metric with an optional delta indicator.
189
190
Parameters:
191
- label: The metric label/name
192
- value: The current metric value
193
- delta: Optional change indicator (number or string)
194
- delta_color: Color for delta ("normal", "inverse", or "off")
195
"""
196
```
197
198
#### Usage Example
199
200
```python
201
import streamlit as st
202
203
# Metrics with deltas
204
col1, col2, col3 = st.columns(3)
205
206
with col1:
207
st.metric("Revenue", "$1.2M", "+12.5%")
208
209
with col2:
210
st.metric("Users", "1,234", "+56")
211
212
with col3:
213
st.metric("Conversion", "3.4%", "-0.2%", delta_color="inverse")
214
```
215
216
### Status Messages and Alerts
217
218
Communicate application status, information, warnings, and errors to users.
219
220
```python { .api }
221
def success(body: str) -> DeltaGenerator:
222
"""
223
Display a success message with green styling.
224
225
Parameters:
226
- body: The success message text
227
"""
228
229
def info(body: str) -> DeltaGenerator:
230
"""
231
Display an informational message with blue styling.
232
233
Parameters:
234
- body: The information message text
235
"""
236
237
def warning(body: str) -> DeltaGenerator:
238
"""
239
Display a warning message with yellow styling.
240
241
Parameters:
242
- body: The warning message text
243
"""
244
245
def error(body: str) -> DeltaGenerator:
246
"""
247
Display an error message with red styling.
248
249
Parameters:
250
- body: The error message text
251
"""
252
253
def exception(exception: Exception) -> DeltaGenerator:
254
"""
255
Display an exception with full traceback.
256
257
Parameters:
258
- exception: The Exception object to display
259
"""
260
```
261
262
#### Usage Example
263
264
```python
265
import streamlit as st
266
267
# Status messages
268
st.success("Operation completed successfully!")
269
st.info("Here's some helpful information.")
270
st.warning("This action cannot be undone.")
271
st.error("An error occurred during processing.")
272
273
# Exception display
274
try:
275
result = 1 / 0
276
except ZeroDivisionError as e:
277
st.exception(e)
278
```
279
280
### Magic Write Function
281
282
Universal display function that intelligently renders different data types.
283
284
```python { .api }
285
def write(*args, **kwargs) -> DeltaGenerator:
286
"""
287
Magic function that displays almost anything.
288
289
Parameters:
290
- *args: Variable arguments of various types to display
291
- **kwargs: Keyword arguments passed to specific display functions
292
293
Supported types:
294
- str: Displays as markdown
295
- pandas.DataFrame: Displays as interactive dataframe
296
- dict/list: Displays as JSON
297
- matplotlib.pyplot.Figure: Displays as chart
298
- PIL.Image: Displays as image
299
- And many more...
300
"""
301
```
302
303
#### Usage Example
304
305
```python
306
import streamlit as st
307
import pandas as pd
308
import matplotlib.pyplot as plt
309
310
# Write can display many different types
311
st.write("Hello **world**!") # Markdown
312
st.write({"key": "value"}) # JSON
313
st.write(pd.DataFrame({"A": [1, 2], "B": [3, 4]})) # DataFrame
314
315
# Multiple arguments
316
st.write("The answer is", 42, "!")
317
```
318
319
### Help and Documentation
320
321
Display help information and documentation for Python objects.
322
323
```python { .api }
324
def help(obj) -> DeltaGenerator:
325
"""
326
Display help documentation for a Python object.
327
328
Parameters:
329
- obj: Any Python object to display help for
330
"""
331
```
332
333
#### Usage Example
334
335
```python
336
import streamlit as st
337
import pandas as pd
338
339
# Display help for a function
340
st.help(pd.DataFrame)
341
342
# Display help for built-in functions
343
st.help(len)
344
```