Is there a way to update images corresponding to a slider in real-time? Currently, I have the following code that is experiencing a lot of lag:
slider8 = record(st.select_slider, "Normal Trendline (After)")
chart_to_show_normal_trend_after = slider8("Normal Trendline Chart (After)", [i for i in range(1,11) for _ in range(5)])
chart = "ny_trendlines/visualization" + str(int(chart_to_show_normal_trend_after) - 1) + ".png"
st.image(chart)
i+=1
I have a bunch of images if Altair charts, and I bind them to the slider, but when I move the slider it takes a long time for the new image to update. I also tried just generating the Altair chart natively inside Streamlit, but the slider is still really slow. Is there a way to get past this lag?
Hi Randy! Thank you for the response. This snippet has everything relevant to the chart, so I am not sure what to add. Let me try and explain it:
I have a local folder called “visualizations” which has a lot of Altair images (labeled 0-9) saved as PNG files. Essentially, I bind the numbers on the slider to the corresponding image. So as I slide the slider, the image updates. The problem is that it updates really slowly, and I am not sure how to make it go faster.
I tried another approach with a different graph where I generated the Altair graph within Streamlit, but that also updated really slowly.
I was more confused about what the record and slider8 functions were doing, but maybe it doesn’t matter.
If you have images to load, I suspect it goes as fast as it can, since it’s reading from disk. If you wrap the image loading code into a function, you can use st.cache() to cache the results. At least in that case, the load will only be slower the first time, then read from RAM in the future (presumably, a lot faster).
On generating the Altair graphs being slow, how much data are you using to generate the graphs?
I will try using st.cache for that. Can you just wrap it around a function call?
It is a fair bit of data, but nothing too crazy. It essentially reads data in from a dataframe that contains a numerical “mobility” value for all 50 states over a period of about 97 days. My slider picks a value n that range, and then I use the slider value to filter out the dataframe so that it only has values for the given day. It then generates a chloropleth map using the data. So, in short, I am trying to make an interactive chloropleth map of the US, and it works, but it updates very slowly. Here is the relevant code snippet:
Yes, make a function that returns each image, then use the @st.cache() decorator.
If you have ~5000 data points and your charts are slow, I’d look in your code and see where improvements can be made. From a quick scan, I suspect the transform_lookup line to be slower than you’d like since it’s a join. Perhaps you can do your join outside of the Altair chart, one time for all of the data, and cache it to get the speed you want.
I have tried this, but then I run into another issue. I was able to generate this interactive map inside a Jupyter notebook by doing the join before the graph generation, and the slider updated quickly.
However, to deal with the data, I have to use geopandas. When I add the code into Streamlit, it gives me a “dtype geometry not understood” error.
Hi,
Has Someone found a workaround for This.
altair has its own slider option, which refreshes chart much faster than st slider. This can be seen, when you execute altair slider in jupyter.
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.