Streamlit Part 4: Mastering Media Elements - Logos, Images, Videos, and Audio
Welcome back to our tutorial series on Streamlit! In this fifth installment, we dive into the exciting realm of media elements, focusing on how to integrate logos, images, videos, and audio into your Streamlit applications with ease.
Introduction
As we continue our journey through Streamlit's capabilities, it's time to elevate the visual and auditory experience of your apps. In this tutorial, we'll start with logos and work our way through images, videos, and audio. By mastering these elements, you can create a more engaging and interactive interface for your users, making your Streamlit applications stand out.
Working with Logos
Logos serve as a visual anchor on your page, offering a quick and easy way to brand your app. Streamlit makes it simple to display logos with an effortless setup. Let's start by adding a logo to our app:
import streamlit as st
st.image("<https://upload.wikimedia.org/wikipedia/commons/e/e1/Logo_of_YouTube_%282015-2017%29.svg>",
width=200,
caption="YouTube Logo")
In this example, we've used the YouTube logo to illustrate how easy it is to embed logos. You can replace the URL with any logo of your choice. The width
parameter allows you to control the size of the logo, while the caption
adds a description below it.
To make your logo clickable, you can wrap it in a Markdown link:
st.markdown("""
[![YouTube Logo](<https://upload.wikimedia.org/wikipedia/commons/e/e1/Logo_of_YouTube_%282015-2017%29.svg>)](<https://www.youtube.com>)
""")
This code creates a clickable logo that directs users to YouTube when clicked.
Displaying Images
Images are not only aesthetic enhancements but can also provide informative visuals to your applications. In Streamlit, image elements work similarly to logos with some extended capabilities. Let's add a scenic image to our app:
st.image("<https://images.unsplash.com/photo-1464822759023-fed622ff2c3b>",
caption="Scenic mountain landscape",
use_column_width=True)
The use_column_width=True
parameter ensures that the image fits the width of its container, which is useful for responsive layouts. You can also specify a custom width if needed.
For multiple images, you can use columns:
col1, col2 = st.columns(2)
with col1:
st.image("image1.jpg", caption="Image 1")
with col2:
st.image("image2.jpg", caption="Image 2")
This code displays two images side by side, each in its own column.
Embedding Videos
To further expand your application's capabilities, let's learn how to embed video content. Streamlit makes it easy to add videos from URLs or local files:
st.video("<https://www.youtube.com/watch?v=dQw4w9WgXcQ>", start_time=3)
This code embeds a YouTube video in your app. The start_time
parameter sets the video to begin playing at the 3-second mark.
For local video files, you can use:
st.video("path/to/your/video.mp4")
Adding Audio
Rounding out our media journey is audio. Integrating sound into your apps can add layers of interaction and information. Here's how to add an audio element to your Streamlit app:
st.audio("<https://upload.wikimedia.org/wikipedia/commons/c/c4/Muriel-Nguyen-Xuan-Chopin-valse-opus64-1.ogg>",
format='audio/ogg',
start_time=0)
This code adds an audio player to your app. The format
parameter specifies the audio format, and start_time
determines where the playback begins.
For local audio files:
st.audio("path/to/your/audio.mp3")
Conclusion and Next Steps
That concludes our discussion on media elements in Streamlit. Armed with the knowledge of integrating logos, images, videos, and audio, your applications can now offer a richer user interaction.
Here's a quick recap of what we've covered:
- Adding and customizing logos
- Displaying and arranging images
- Embedding videos from URLs and local files
- Integrating audio elements
In the upcoming tutorials, we'll explore more advanced topics and start building interactive applications. We'll dive deeper into Streamlit's widgets, state management, and how to create dynamic, data-driven apps.
Whether you're new to Streamlit or a seasoned developer, incorporating these media elements will undoubtedly enhance your app's functionality and appeal. Keep practicing and experimenting with these features to fully harness the power of Streamlit in your projects.
Stay tuned for our next installment, where we'll take your Streamlit skills to the next level. Happy coding, and see you next time!
🔗 Get the Code: GitHub - jamesbmour/blog_tutorials
🔗 Related Streamlit Tutorials:JustCodeIt
🍻 Support my work: Buy Me a Coffee
Top comments (0)