EP5. Adding Media Files in our Streamlit Web App

You can add images using URLs too!!!!

Adith - The Data Guy
Analytics Vidhya

--

Hey Guys!! Welcome back to the EP5 on our basics of Streamlit series. Today in this article, we are going to look at how to add media files such as images, videos, and audio files inside our Streamlit web app, and without any further ado, let’s get started!!!

Image by Author

As usual, we will start with importing all the required packages. As we are dealing with streamlit we will import streamlit and also we need to import Image files to do that we will also import PIL which stands for Python Imaging Library which is a famous package that is used when we are about to use images in our program.

import streamlit as stfrom PIL import Image

Adding an Image

We will start by importing an image. The most important point here is to have your media files in the same directory where you have your raw Python code.

First, we will create a variable “img” in which we will use our open() from our image module in PIL to open the image. My open() will take a parameter that is the name of the file.

img = Image.open("st.jpg")

The next step is to display this image inside our streamlit and so, now we will use the image() from our streamlit to do this. This image() will take the variable name in which we have stored our image. i.e, “img”

st.image(img)

We can also add an image directly from the web too. To do that instead of passing the variable inside our image() just pass the URL of the image.

st.image("https://www.pexels.com/photo/landscape-nature-sky-man-6620743/")

If you want to look at the video version of this article check this video 🤗👆.

Adding a Video

To add a video we have to use our open() pass in the name of our video file and use the “rb” mode that is read in binary mode. And later we have to display it in our streamlit so, use the video(), in our streamlit and pass in the variable in which you saved your opened video file.

You can also use the parameter “start_time” and pass in the value of the second from which you have to start your video.

video1 = open("Day83.mp4", "rb") 
st.video(video1)
#st.video(video1, start_time = 25)

Adding an Audio

To add audio we have to use our open() pass in the name of our audio file and use the “rb” mode that is read in binary mode. And later we have to display it in our streamlit so, use the audio(), in our streamlit and pass in the variable in which you saved your opened video file.

audio1 = open("audio.mp3", "rb")
st.audio(audio1)

EP4 of the series is an interactive episode where I taught how to add widgets in our Streamlit web app, learning to do that via a blog article might be overwhelming so, so kindly check that video here.

Conclusion

With this, we are concluding this article. And today in this article we looked at how to add the media file in our streamlit.

Don’t forget to leave your responses.✌

Everyone stay tuned!! To get my stories in your mailbox kindly subscribe to my newsletter. Thank you for reading! Do not forget to give your claps and share your responses and share them with a friend.

This article was published earlier on Fittechie. in

--

--

Adith - The Data Guy
Analytics Vidhya

Passionate about sharing knowledge through blogs. Turning data into narratives. Data enthusiast. Content Curator with AI. https://www.linkedin.com/in/asr373/