In Python, Streamlit allows us to easily embed images, videos, and audio files into our Streamlit web apps.
To add media elements, we’ll first import the streamlit
module with the following command:
import streamlit as st
It displays an image or list of images.
st.image(image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
image
: This can be a monochrome image or a color image, or an RGBA image. It may be a URL to fetch the image or a path for the local image files, or an SVG XML string, or a list to display multiple images.
caption
: This is an image caption for a single image, but if displaying multiple images, the caption will be a list of captions for each image.
width
: This is the image width. If we pass None
, then it means it will use image width but not exceed the width of the column.
use_column_width
:
If we set auto
, it will be set as the image’s width to its natural size. If we set always
or True
, it will set the image’s width to the column width. If we set never
or False
, it means it will set the image’s width to its natural size.
clamp
: This will clamp the image pixel values to a valid range [0-255].
channels
: If image
is an nd.array
, then this parameter denotes the format used to represent color information.
output_format
: This parameter specifies the format to use when transferring the image data.
Let’s run the app below to first read the image and then display it on the Streamlit web interface.
import streamlit as st from PIL import Image image = Image.open('educativeImage.jpg') st.image(image, caption='Educative')
streamlit
module.Image
module for image io
.It displays an audio player.
st.audio(data, format="audio/wav", start_time=0)
data
: It can be a file name, a URL pointing to the file to load, and Raw audio data.
start_time
: This is the time from which this element should start playing.
format
: This is the type of audio file. The default type is audio/wav
.
Let’s run the app below to play an audio file on the Sreamlit web interface.
import streamlit as st audio_file = open('audio.mp3', 'rb') audio_bytes = audio_file.read() st.audio(audio_bytes, format='audio/mp3')
streamlit
module.audio.mp3
.It displays a video player.
st.video(data, format="video/mp4", start_time=0)
data
: This can be a file name, a URL pointing to the file to load, and Raw video data.
start_time
: This is the time from which this element should start playing.
format
: This is the type of video file. The default type is video/mp4
.
Let’s run the app below to play a video file on the Streamlit web interface.
import streamlit as st video_file = open('video.mp4', 'rb') video_bytes = video_file.read() st.video(video_bytes)
streamlit
module.video.mp4
.Free Resources