Cache Videos/Gifs

Hi,

Question: Is there any way to cache videos/gifs in streamlit. In my case, I have some gifs which i am showing in a streamlit ui page. But whenever i open the page, It is consuming my internet. Upon researching, I found out that st.cache_data caches data on server side. So is there any way to load/cache gifs without requesting streamlit server everytime i open/load the page.

Code which i used:

show_gif - helper function.

def show_gif(file_name: str) -> Any:
    """Convert .gif content into base64 encoded format.

    Parameters
    ----------
    file_name: str
        file_name - Location of the gif.

    Returns
    -------
        Return base64 encoded string format of the gif provided to the function.

    """
    file = open(file_name, "rb")
    contents = file.read()
    data_url = base64.b64encode(contents).decode("utf-8-sig")
    file.close()
    return data_url 

Displaying the GIF:

st.markdown(
        f'<img src="data:image/gif;base64,{show_gif("./assets/gifs/local_gif.gif")}" width="750" height="444">',
        unsafe_allow_html=True,
    )

Hi @Pradeep_Reddy,

Thanks for sharing your question with the community! :balloon: Please update your debugging post to include a code snippet and a link to your app’s public GitHub repo – this will allow the community to help you find an answer as quickly as possible. In the meantime, this post will be tagged as needs-more-info.

Converted gifs to videos(mp4) and used st.video component to display video.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.