pypi-streamlit

Description
A faster way to build and share data apps
Author
tessl
Last updated

How to use

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

media-status.md docs/

1
# Media and Status
2
3
Media display capabilities and status messaging functions for rich content presentation and user feedback. These components enable multimedia content display and effective user communication.
4
5
## Capabilities
6
7
### Media Display
8
9
Display images, audio, video, and PDF content with various formatting options.
10
11
```python { .api }
12
def image(image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto", use_container_width=False):
13
"""
14
Display an image or list of images.
15
16
Parameters:
17
- image: Image data (PIL Image, numpy array, file path, URL, or bytes)
18
- caption (str or list): Image caption(s)
19
- width (int): Image width in pixels
20
- use_column_width (bool): Deprecated - use use_container_width instead
21
- clamp (bool): Clamp image pixel values to valid range
22
- channels (str): Color channel interpretation ('RGB' or 'BGR')
23
- output_format (str): Output format ('JPEG', 'PNG', or 'auto')
24
- use_container_width (bool): Use full container width
25
26
Returns:
27
None
28
"""
29
30
def audio(data, format="audio/wav", start_time=0, sample_rate=None, end_time=None, loop=False, autoplay=False):
31
"""
32
Display an audio player widget.
33
34
Parameters:
35
- data: Audio data (file path, URL, bytes, or numpy array)
36
- format (str): Audio format MIME type
37
- start_time (int): Start playback time in seconds
38
- sample_rate (int): Sample rate for numpy array data
39
- end_time (int): End playback time in seconds
40
- loop (bool): Loop audio playback
41
- autoplay (bool): Start playing automatically
42
43
Returns:
44
None
45
"""
46
47
def video(data, format="video/mp4", start_time=0, subtitles=None, end_time=None, loop=False, autoplay=False, muted=False):
48
"""
49
Display a video player widget.
50
51
Parameters:
52
- data: Video data (file path, URL, or bytes)
53
- format (str): Video format MIME type
54
- start_time (int): Start playback time in seconds
55
- subtitles (str or list): Subtitle file path(s) or WebVTT content
56
- end_time (int): End playback time in seconds
57
- loop (bool): Loop video playback
58
- autoplay (bool): Start playing automatically
59
- muted (bool): Start with audio muted
60
61
Returns:
62
None
63
"""
64
65
def pdf(data, width=None, height=600):
66
"""
67
Display a PDF viewer widget.
68
69
Parameters:
70
- data: PDF data (file path, URL, or bytes)
71
- width (int): Viewer width in pixels
72
- height (int): Viewer height in pixels
73
74
Returns:
75
None
76
"""
77
```
78
79
### Status Messages
80
81
Provide user feedback with different severity levels and styling.
82
83
```python { .api }
84
def success(body, icon=True):
85
"""
86
Display a success message with green styling.
87
88
Parameters:
89
- body (str): Success message text
90
- icon (bool or str): Show success icon or custom icon name
91
92
Returns:
93
None
94
"""
95
96
def info(body, icon=True):
97
"""
98
Display an informational message with blue styling.
99
100
Parameters:
101
- body (str): Information message text
102
- icon (bool or str): Show info icon or custom icon name
103
104
Returns:
105
None
106
"""
107
108
def warning(body, icon=True):
109
"""
110
Display a warning message with yellow styling.
111
112
Parameters:
113
- body (str): Warning message text
114
- icon (bool or str): Show warning icon or custom icon name
115
116
Returns:
117
None
118
"""
119
120
def error(body, icon=True):
121
"""
122
Display an error message with red styling.
123
124
Parameters:
125
- body (str): Error message text
126
- icon (bool or str): Show error icon or custom icon name
127
128
Returns:
129
None
130
"""
131
132
def exception(exception):
133
"""
134
Display an exception with full traceback.
135
136
Parameters:
137
- exception (Exception): Exception object to display
138
139
Returns:
140
None
141
"""
142
```
143
144
### Interactive Notifications
145
146
Display temporary notifications and celebratory animations.
147
148
```python { .api }
149
def toast(body, icon=None):
150
"""
151
Display a toast notification that appears temporarily.
152
153
Parameters:
154
- body (str): Toast message text
155
- icon (str): Optional icon name or emoji
156
157
Returns:
158
None
159
"""
160
161
def balloons():
162
"""
163
Display celebratory balloons animation.
164
165
Returns:
166
None
167
"""
168
169
def snow():
170
"""
171
Display falling snow animation.
172
173
Returns:
174
None
175
"""
176
```
177
178
### User Feedback Widgets
179
180
Collect user feedback and interaction responses.
181
182
```python { .api }
183
def feedback(options, key=None, disabled=False, on_change=None, args=None, kwargs=None):
184
"""
185
Display a feedback widget for collecting user sentiment.
186
187
Parameters:
188
- options (str): Type of feedback options ('thumbs' or 'faces')
189
- key (str): Unique key for the widget
190
- disabled (bool): Disable the widget
191
- on_change (callable): Function to call when feedback changes
192
- args (tuple): Arguments to pass to on_change function
193
- kwargs (dict): Keyword arguments to pass to on_change function
194
195
Returns:
196
dict or None: Feedback score and sentiment information
197
"""
198
199
def badge(label, background_color=None):
200
"""
201
Display a badge element with optional color.
202
203
Parameters:
204
- label (str): Badge text
205
- background_color (str): Badge background color (hex, CSS name, or None)
206
207
Returns:
208
None
209
"""
210
```
211
212
## Usage Examples
213
214
### Image Display
215
216
```python
217
import streamlit as st
218
from PIL import Image
219
import numpy as np
220
221
# Display image from file
222
st.subheader("Image from File")
223
try:
224
image = Image.open("sample_image.jpg")
225
st.image(image, caption="Sample Image", use_container_width=True)
226
except FileNotFoundError:
227
st.error("Image file not found")
228
229
# Display image from URL
230
st.subheader("Image from URL")
231
st.image(
232
"https://static.streamlit.io/examples/cat.jpg",
233
caption="Cat from URL",
234
width=300
235
)
236
237
# Display numpy array as image
238
st.subheader("Generated Image")
239
image_array = np.random.randint(0, 255, (200, 200, 3), dtype=np.uint8)
240
st.image(image_array, caption="Random noise image", clamp=True)
241
242
# Display multiple images
243
st.subheader("Image Gallery")
244
images = [
245
"https://static.streamlit.io/examples/cat.jpg",
246
"https://static.streamlit.io/examples/dog.jpg"
247
]
248
captions = ["Cat", "Dog"]
249
st.image(images, caption=captions, width=200)
250
```
251
252
### Audio and Video
253
254
```python
255
# Display audio player
256
st.subheader("Audio Player")
257
258
# From file
259
try:
260
audio_file = open("sample_audio.wav", "rb")
261
audio_bytes = audio_file.read()
262
st.audio(audio_bytes, format="audio/wav")
263
audio_file.close()
264
except FileNotFoundError:
265
st.info("Audio file not available for demo")
266
267
# From URL
268
st.audio("https://www.soundjay.com/misc/sounds/bell-ringing-05.wav")
269
270
# Video player
271
st.subheader("Video Player")
272
273
# Local video file
274
try:
275
video_file = open("sample_video.mp4", "rb")
276
video_bytes = video_file.read()
277
st.video(video_bytes, start_time=10) # Start at 10 seconds
278
video_file.close()
279
except FileNotFoundError:
280
st.info("Video file not available for demo")
281
282
# Video with subtitles
283
st.video(
284
"sample_video.mp4",
285
subtitles="subtitles.vtt",
286
loop=True,
287
autoplay=False
288
)
289
```
290
291
### PDF Viewer
292
293
```python
294
# Display PDF document
295
st.subheader("PDF Document")
296
297
try:
298
with open("document.pdf", "rb") as pdf_file:
299
pdf_bytes = pdf_file.read()
300
301
st.pdf(pdf_bytes, width=700)
302
except FileNotFoundError:
303
st.info("PDF file not available for demo")
304
305
# PDF from URL
306
st.pdf("https://example.com/sample.pdf", height=400)
307
```
308
309
### Status Messages
310
311
```python
312
# Different types of status messages
313
st.subheader("Status Messages")
314
315
st.success("✅ Operation completed successfully!")
316
st.info("ℹ️ Here's some helpful information.")
317
st.warning("⚠️ Please review your input carefully.")
318
st.error("❌ An error occurred during processing.")
319
320
# Status messages with custom icons
321
st.success("Data saved!", icon="💾")
322
st.info("New features available", icon="🚀")
323
st.warning("Server maintenance scheduled", icon="🔧")
324
st.error("Connection failed", icon="🌐")
325
326
# Exception display
327
try:
328
result = 1 / 0
329
except ZeroDivisionError as e:
330
st.exception(e)
331
```
332
333
### Toast Notifications
334
335
```python
336
# Toast notifications (appear temporarily)
337
if st.button("Show Success Toast"):
338
st.toast("Success! Operation completed.", icon="✅")
339
340
if st.button("Show Info Toast"):
341
st.toast("Information: Check your email.", icon="📧")
342
343
if st.button("Show Warning Toast"):
344
st.toast("Warning: Please save your work.", icon="⚠️")
345
346
if st.button("Show Custom Toast"):
347
st.toast("Custom notification!", icon="🎉")
348
```
349
350
### Celebrations
351
352
```python
353
# Celebratory animations
354
col1, col2 = st.columns(2)
355
356
with col1:
357
if st.button("🎈 Celebrate with Balloons"):
358
st.balloons()
359
st.success("Congratulations! 🎉")
360
361
with col2:
362
if st.button("❄️ Let it Snow"):
363
st.snow()
364
st.info("Winter wonderland! ⛄")
365
```
366
367
### User Feedback Collection
368
369
```python
370
# Feedback widgets
371
st.subheader("Feedback Collection")
372
373
# Thumbs up/down feedback
374
feedback_1 = st.feedback("thumbs", key="feedback_1")
375
if feedback_1:
376
st.write(f"Feedback received: {feedback_1}")
377
378
# Faces feedback (1-5 scale)
379
feedback_2 = st.feedback("faces", key="feedback_2")
380
if feedback_2:
381
st.write(f"Rating: {feedback_2['score']}/5")
382
383
# Badge display
384
st.subheader("Badges")
385
col1, col2, col3 = st.columns(3)
386
387
with col1:
388
st.badge("New", background_color="#FF6B6B")
389
390
with col2:
391
st.badge("Premium", background_color="#4ECDC4")
392
393
with col3:
394
st.badge("Beta", background_color="#45B7D1")
395
```
396
397
### Media with Interactive Controls
398
399
```python
400
# Interactive media controls
401
st.subheader("Interactive Media Player")
402
403
# Media controls
404
col1, col2 = st.columns([3, 1])
405
406
with col1:
407
# Video player with user controls
408
autoplay = st.checkbox("Autoplay")
409
loop_video = st.checkbox("Loop")
410
muted = st.checkbox("Muted")
411
412
start_time = st.slider("Start time (seconds)", 0, 300, 0)
413
414
with col2:
415
st.write("**Player Settings**")
416
417
if st.button("Load Video"):
418
st.video(
419
"sample_video.mp4",
420
autoplay=autoplay,
421
loop=loop_video,
422
muted=muted,
423
start_time=start_time
424
)
425
```
426
427
### Status Message with Actions
428
429
```python
430
# Status messages with follow-up actions
431
error_occurred = st.checkbox("Simulate error condition")
432
433
if error_occurred:
434
st.error("Database connection failed!", icon="🔌")
435
436
col1, col2, col3 = st.columns(3)
437
438
with col1:
439
if st.button("Retry Connection"):
440
st.success("Connection restored!")
441
442
with col2:
443
if st.button("Check Status"):
444
st.info("System status: All services operational")
445
446
with col3:
447
if st.button("Contact Support"):
448
st.info("Support ticket created: #12345")
449
450
# Success with next steps
451
if st.button("Complete Process"):
452
st.success("✅ File uploaded successfully!")
453
st.balloons()
454
455
with st.expander("Next Steps"):
456
st.write("1. Review the uploaded data")
457
st.write("2. Configure processing options")
458
st.write("3. Start the analysis")
459
```
460
461
### Conditional Media Display
462
463
```python
464
# Conditional media based on user selection
465
media_type = st.selectbox(
466
"Select media type:",
467
["None", "Image", "Audio", "Video", "PDF"]
468
)
469
470
if media_type == "Image":
471
st.image(
472
"https://static.streamlit.io/examples/cat.jpg",
473
caption="Sample image",
474
use_container_width=True
475
)
476
477
elif media_type == "Audio":
478
st.audio("https://www.soundjay.com/misc/sounds/bell-ringing-05.wav")
479
480
elif media_type == "Video":
481
st.video("https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_1mb.mp4")
482
483
elif media_type == "PDF":
484
st.info("PDF viewer would display here with actual PDF file")
485
486
else:
487
st.info("Select a media type to display content")
488
```