Location>code7788 >text

『Playing with Streamlit』 - Image and Media Components

Popularity:502 ℃/2024-11-16 19:45:02

StreamlitThe image and media components in thecap (a poem)

They are designed for use inStreamlit WebDesigned for embedding and displaying multimedia content in applications, these components not only enrich the presentation of applications, but also greatly enhance the user experience and interactivity.

1.

function is used in theStreamlitImage content is displayed in the application to enhance the visual presentation.

For example, in a data visualization scenario byDisplay the results of data analysis, such as bar charts, line graphs, etc;

User understanding can also be enhanced by embedding product images, schematics, etc. in the project presentation.

Its main parameters are:

name (of a thing) typology clarification
image / [] / BytesIO / str / [str] To display an image, you can also specify an image URL or a list of URLs
caption str/[str] Image title, which should be a list of strings if multiple images are displayed
width int / None Image width, None means use the width of the image itself
clamp bool Whether or not to compress the pixel value of the image to the valid field (0~255), only valid for byte array images
channels "RGB" / "BGR" Image Channel Type
output_format "JPEG" / "PNG" / "auto" image format
use_container_width bool If set to True, the column width is used as the image width

1.1 Examples of use

Display local image files byimageparameter accepts a PIL image object.captionparameter to add a caption to the image.

widthparameter sets the display width of the image.

import streamlit as st
from PIL import Image

# Read a local image file
image = ("path/to/your/")

# Use the display image and set the caption and width
(image, caption="This is a local image", width=300)

showcaseURLimage and use theuse_column_widthparameter to make the image width adaptiveStreamlitThe width of the column.

# image_url
image_url = "/"

# Use the display URL image and set the column width to be used
(
    image_url,
    use_column_width=True,
    caption="This is a web image".
)

The width of the image changes adaptively with the size of the browser window.

2.

function is used in theStreamlitEmbedded audio content in applications to add sound effects to data analysis results for enhanced presentation.

Its main parameters are:

name (of a thing) typology clarification
data str / bytes / BytesIO / / file Audio data to be played, either as a byte stream, or as an open file
format str MIME type of the audio file, such as 'audio/ogg', 'audio/mp3', etc.
start_time int / float / timedelta / str / None Specify the start time of audio playback
sample_rate int / None The sample rate of the audio. When playing audio, you usually don't need to set this parameter directly.
end_time int / float / timedelta / str / None Specify the end time of audio playback
loop bool Whether to loop the audio
autoplay bool Whether to play audio automatically

2.1 Examples of use

playback of local audio files.autoplayThe parameters are set toTrueto make the audio play automatically.

import streamlit as st

# Open a local audio file
audio_file = open("path/to/your/audio.mp3", "rb")
audio_bytes = audio_file.read()

# Use playback audio and autoplay
(
    audio_bytes,
    format="audio/mp3",
    autoplay=True,
)

It will play automatically after you run it.

cap (a poem)Likewise, audio can be passed into the URL and played directly online.

3.

function is used in theStreamlitVideo content is embedded in the application to demonstrate the characteristics of the analysis results or the analysis process through video.

Its main parameters are:

name (of a thing) typology clarification
data str / bytes / BytesIO / / file Video data to be played, either as a URL string, a byte stream, or an open file
format str MIME type of the video file, e.g. 'video/mp4', default value is 'video/mp4'
start_time int / float / timedelta / str / None The time when the video starts playing
subtitles str / bytes / Path / / dict Video Subtitle Data
end_time int / float / timedelta / str / None Video end playback time
loop bool Whether to loop the video
autoplay bool Whether or not to play video automatically
muted bool Mute playback or not

3.1 Examples of use

Play local video files, bystart_timecap (a poem)end_timeparameter sets the time (in seconds) at which video playback begins and ends.

import streamlit as st

# Open a local video file
video_file = open('path/to/your/video.mp4', 'rb')
video_bytes = video_file.read()

# Use to play the video and set the title and start time
(
    video_bytes,
    format="video/mp4",
    start_time=5,
    end_time=10, )
)

After running, the video loads and stops at5sposition, click Play, and play to the10sAutomatically stops when

4. Summary

In conclusion.cap (a poem)Together, these three components form a powerful multimedia presentation toolset in Streamlit.

They each focus on the display and playback of images, audio and video, and provide developers with great convenience and creative space through rich parameter settings and flexible embedding methods.

Whether you are building a data visualization application, a product showcase page or an online education platform, these components help developers easily embed and present multimedia content.