One of the great advantages of Streamlit is the simplicity of its functions for displaying different types of media, such as images, videos, and audio. Let's learn how to use them practically with examples.

Media Functions in Streamlit

st.image()

This function is used to display images in the application. You can adjust the size and add captions to images.

Example:

import streamlit as st

st.image("nature.jpg", caption="A beautiful landscape", width=500)

st.audio()

Displays an integrated audio player to play MP3 or WAV files.

Example:

st.audio("music.mp3", start_time=10)

Here, the audio starts playing from the tenth second.

st.video()

Allows you to display videos directly in the application, supporting formats such as MP4.

Example:

st.video("presentation_video.mp4")

Complete Example Code

Here is a complete example using all three functions:

import streamlit as st

# Application Title
st.title("Displaying Media with Streamlit")

# Displaying an image
st.image("flowers.jpg", caption="Flowers in a field", use_column_width=True)

# Displaying an audio player
st.audio("podcast_intro.mp3", start_time=5)

# Displaying a video
st.video("tutorial_video.mp4")

# Additional message
st.caption("The files used here are fictitious examples. Replace them with your own files.")

Important Tips

  • Ensure that media files are in the same directory as the script or use absolute paths to access them.

  • Streamlit supports a variety of common media formats, such as:

    • Images: JPEG, PNG, GIF.

    • Audio: MP3, WAV.

    • Video: MP4, MOV, WebM.

With these functions, you can create dynamic presentations, media analysis applications, or even interactive portfolios.