I run a script with a few caches.
When I use the following code it clears all the caches:
from streamlit import caching
caching.clear_cache()
I would like to clear only a specific cache. How can I do this?
I run a script with a few caches.
When I use the following code it clears all the caches:
from streamlit import caching
caching.clear_cache()
I would like to clear only a specific cache. How can I do this?
Hey @theletz, welcome to Streamlit!
This isn’t currently (easily) doable. Can you give some context for what you’re trying to achieve? We have some caching improvements in the pipeline, but it may also be the case that there’s an easier workaround for your use case. Thanks!
Actually, I tried to save the last choice.
For example:
x = st.sidebar.slider("Choose x value: ", min_x, max_x)
y_list = change_y_list(x)
y = st.sidebar.selectbox("Choose y value: ", options=y_list, index=y_idx)
if x was changed, the default value of y_idx will be 0.
I want to check if the previous choice of y is in y_list, and if so, to take the index of the previous choice as the index.
The only way I found to do this is using allow_output_mutation=True, like this:
@st.cache(allow_output_mutation=True)
def get_y_ids():
return []
def get_y_idx(last_y_ids, y_list):
if not last_y_ids:
y_idx = 0
else:
last_y_id = last_y_ids[-1]
if last_y_id in y_list:
y_idx = y_list.index(last_y_id)
else:
y_idx = 0
return y_idx
if name == '__main__':
x = st.sidebar.slider("Choose x value: ", min_x, max_x)
y_list = change_y_list(x)
cached_y_ids = get_y_ids()
y_idx = get_y_idx(cached_y_ids, y_list)
y = st.sidebar.selectbox("Choose y value: ", options=y_list, index=y_idx)
cached_y_ids.append(y_id)
I just thought:
Hi @theletz,
this is really interesting. Let me know if my understanding is correct. You are trying to store a mutable object in the st.cache
, so that you can use this object to store a value that lives across runs. Specifically, you want to keep track of the “last choice” of a widget. allow_output_mutation=True
allows you to do so, as you pointed out.
This snipped is doing the above:
import streamlit as st
@st.cache(allow_output_mutation=True)
def get_mutable():
return []
x = st.sidebar.slider("Choose x value: ", 0, 10)
mutable_object = get_mutable()
mutable_object.append(x)
st.write(f"Choice history is {mutable_object}")
Matteo
Yes, that`s exactly what I meant.
Now, I just wondered:
Hey @theletz,
1. Yes, you could do it using @tvst’s SessionState module
import streamlit as st
# SessionState module from https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92
import SessionState
state = SessionState.get(slider_history=[])
def addToHistory(value):
if len(state.slider_history) < 1 or state.slider_history[-1] != value:
state.slider_history.append(value)
if st.button("Clear history"):
state.slider_history.clear()
value = st.sidebar.slider("Choose a value", 0, 10)
addToHistory(value)
st.write(f"Choise history is {state.slider_history}")
2. As you are using a mutable object you could use .clear() and that’s it, for example:
import streamlit as st
@st.cache(allow_output_mutation=True)
def get_mutable():
return []
mutable_object = get_mutable()
if st.button("Clear history cache"):
mutable_object.clear()
x = st.sidebar.slider("Choose x value: ", 0, 10)
if len(mutable_object) < 1 or x != mutable_object[-1]:
mutable_object.append(x)
st.write(f"Choice history is {mutable_object}")
hello @monchier,
thank you for sharing the awesome code with us. In my project, I need to reuse my cache data by using multi-select or any other widgets. I’m using text_input and multi-select in a single function. what I’m trying to do is just collecting the cache data from text_input and want to use that in multi-select. but I still don’t know how to do that. could you please help me.
any way to clear the cache created as part of that session.
Hey @Revathi, welcome to the forums!
To programmatically clear the cache from Python you could do the following
from streamlit import caching
caching.clear_cache()
There’s also a Clear Cache
item in the Hamburger Menu in the app, just FYI.
Hi @arraydude
@tim
Thanks for the reply.
After the user logins to my app, after the first attempt gets failed because of the server issue, he hasn’t not able to get the results although its working fine. I suspect its because of the caching, and I just want to clear the cache for that particular selections. Is there any possible way to mitigate it.
Hi @Revathi,
Could you give us a small code example with your issue? So we can help you better or even better create a new thread on the forum.
I am using the streamlit version 1.1.0. Is the clear_cache() api still available in this version? It seems to be not there anymore.
Hello, in 1.2.0 st.legacy_caching.caching.clear_cache()
is available. I suppose that the new caching system will change this later.
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.
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.
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.
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.