I was wondering, what’s the speediest way to display 2D/image data using streamlit?
I have a 3D Ultrasound image that I traverse through the frames using a slider, however the image display between frames have this “fade transition”. Is there anyway to avoid the fade transition and show the frames faster while you traverse between frames?
The best way around this is to wrap image retrieval in a caching function. The initial retrieval will be slow (if the image is large or obtained over a network) and the previous image will grey out before it is replaced. Once the images have been cached then subsequent displays will be fast.
Here’s a program that simulates this:
import time
import streamlit as st
IMAGES = [
"https://unsplash.com/photos/GJ8ZQV7eGmU/download?force=true&w=1920",
"https://unsplash.com/photos/eHlVZcSrjfg/download?force=true&w=1920",
"https://unsplash.com/photos/zVhYcSjd7-Q/download?force=true&w=1920",
"https://unsplash.com/photos/S5uIITJDq8Y/download?force=true&w=1920",
"https://unsplash.com/photos/E4bmf8BtIBE/download?force=true&w=1920",
]
# set ttl to None if you don't want to refresh the cache
@st.experimental_memo(ttl=60, show_spinner=True)
def get_cached_image(slide_collection_key, index):
# slide_collection_key is used by st cache functions
# sleep simulates a slow network so we can see the st.image ghosting effect
time.sleep(2)
return IMAGES[index]
def slideshow(images):
# Generate a session state key based on images.
slide_collection_key = f"slideshow_{str(images).encode().hex()}"
# Initialize the default slideshow index.
if 'index' not in st.session_state:
st.session_state['index'] = 0
# Get the current slideshow index.
def _index_change_cb(value):
index = abs((st.session_state['index'] + value) % len(images))
st.session_state['index'] = index
index = st.session_state['index']
st.image(get_cached_image(slide_collection_key, index))
st.caption(f'{index}')
c1, c2, _c3 = st.columns([1,1,6])
c1.button('Previous', on_click=_index_change_cb, args=(-1,))
c2.button('Next', on_click=_index_change_cb, args=(1,))
st.title("Streamlit Slideshow")
slideshow(IMAGES)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.