Button to clear cache and rerun

Is there a way to create a button on my page that does the following (a) clear cache and (b) rerun?

3 Likes

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!

4 Likes

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.

3 Likes

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! :wave:

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 on localhost 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! :balloon:
Snehan

2 Likes

the clear cache button, sometimes is useful to end users.
canā€™t i show them in the "viewer experience?

3 Likes

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.

4 Likes

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'

4 Likes

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.

2 Likes

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.

4 Likes

Thank you all for your honest feedback! Iā€™m relaying it to the Streamlit dev team :balloon:

1 Like

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.

3 Likes

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