Is there a way to create a button on my page that does the following (a) clear cache and (b) rerun?
Thanks! That is what I looking to automate in my script based on certain conditions. Are there commands for this in the API?
Hi,
There are some options to do this programatically (see these two issues clear cache and rerun):
To clear cache:
from streamlit import caching
caching.clear_cache()
To rerun script
from streamlit.ScriptRunner import RerunException
raise RerunException
Hope it helps!
Why doesnāt this work? I think the cache isnāt cleared and I donāt see the balloons
import streamlit as st
import time
from helpers import *
from streamlit.script_runner import RerunException
@st.cache(suppress_st_warning=True) # š Changed this
def expensive_computation(a, b):
# š Added this
st.write("Cache miss: expensive_computation(", a, ",", b, ") ran")
time.sleep(2) # This makes the function take 2s to run
return a * b
a = 2
b = 21
res = expensive_computation(a, b)
st.write("Result:", res)
my_slot0 = st.sidebar.empty()
my_slot1 = st.sidebar.empty()
my_slot0.info("Clear cache")
if my_slot1.button("Clear"):
my_slot0.error("Do you really, really, wanna do this?")
if my_slot1.button("Yes I'm ready to rumble"):
caching.clear_cache()
st.balloons()
my_slot0.error("Cache is cleared, please reload to scrape new values")
time.sleep(10)
if my_slot1.button("reload"):
raise RerunException
module āstreamlit.cachingā has no attribute āclear_cacheā
???
What version of Streamlit are you using?
0.89
@rcsmit Aside from the caching - this script cannot work technically. You are nesting buttons. Buttons trigger reruns. Clicking the first button creates the second button. So far so good. Clicking the second button will again trigger a rerun. This time the first button is NOT clicked. So there is no way you can get two consecutive buttons to evaluate to TRUE in one run - you can only click one button per run. Do not nest buttons. You could use a checkbox instead, its state is persisted across reruns. I hope this makes sense for you. If you have questions, please ask.
I also do not see the clear_cache
method on streamlit 0.89. Has it moved or is it a bug?
I notice the button is also gone from the hamburger menu in the app (although hitting the c key still works).
Hi @Hayk_Martiros,
Welcome to the Streamlit community!
The clear_cache
method has moved to streamlit.legacy_caching.clear_cache
.
With the 0.89.0 release, we have updated the configurable hamburger menu options!
The hamburger menu is now divided into the main section (visible to everyone) and the developer section (visible to developers only).
The developer section is context-aware. It appears if the app is running onlocalhost
or if it was deployed in Streamlit Cloud (and if the current user is the appās developer)
Here is what it looks like: The hamburger menu is now divided into the main section (visible to everyone) and the developer section (visible to developers only).
Here is what it looks like:
In light of the context-aware visual redesign, youāll see the Clear cache
button when viewing the app as a developer.
Happy Streamlit-ing!
Snehan
the clear cache button, sometimes is useful to end users.
canāt i show them in the "viewer experience?
It is at times useful for me to clear cache programatically. Please be sure not to remove that option and make it front end driven.
Indeed. I have a covid dashboard which loads lots of files. And sometimes I want to clear the cache if I am on another computer without being logged in; I set the end date on a secret date and the the clear cache function startsā¦
And now I get
AttributeError: module 'streamlit' has no attribute 'legacy_caching'
I totally agree. I always looked at Streamlit as a simple option to āappifyā a notebook and make it accessible to others. A core feature for that would be to cache the latest result and have an option to rerun everything at once (similar to what is possible with a notebook). I mimicked this behavior by using the clear_cache() function, but thatās not possible anymore. How should I approach this use case? Why is it not supported? I honestly fail to understand what Streamlit is if such an obvious feature is not included as one of the core features and workarounds are being removed.
Perhaps Iām approaching this use case in the wrong way, Iād love to be educated.
I would also like to point out the importance of being able to clear cache through a button. Without it, my end users will come to me asking to do it, which eliminates the entire point of having an automated dashboard.
Thank you all for your honest feedback! Iām relaying it to the Streamlit dev team
Can you share the current considerations about caching? Why is it the way it currently is? I want to understand how Streamlit should be used according to the developers.
I would also love to have some kind of workaround solution to allow users to reset the cache. Itās definitely been an issue for some of the apps I work on.
Clear cache is pretty much essential to my Streamlit end-user experience, and having it the hamburger menu was really nice as it didnāt take up any real estate. I would prefer if it was back.
@kevinlinxc @fdubinski you can now achieve the same thing by using
caching.clear_memo_cache()
caching.clear_singleton_cache()
Source: here