Translating st.cache allow_output_mutation=True for new cache_data & cahce_resource functions

if I previously had code like below

@st.cache(allow_output_mutation=True)
def get_manager():
    return stx.CookieManager()

for context I am looking to use cookie manger from library GitHub - Mohamed-512/Extra-Streamlit-Components: An all in one place, to find complex or just not available components by default on streamlit. (as posted about in Cookies support in Streamlit!)

what is the recommended use translation of @st.cache(allow_output_mutation=True) with new caching functions (ie cache_data, cache_resource)? my guess was st.cache_resource, but if use that get warning and errors below

warning

CachedStFunctionWarning: Your script uses st.component_instance() to write to your Streamlit app from within some cached code at get_manager(). This code will only be called when we detect a cache "miss", which can lead to unexpected results.

How to fix this:

Move the st.component_instance() call outside get_manager().
Or, if you know what you're doing, use @st.cache_resource(suppress_st_warning=True) to suppress the warning.
Traceback:
File "C:\Program Files\Python39\lib\threading.py", line 912, in _bootstrap
    self._bootstrap_inner()
File "C:\Program Files\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
File "C:\Program Files\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
File "C:\Users\kdaftari\Code\barepos\dashboards\cookie_dash.py", line 11, in <module>
    cookie_manager = get_manager()
File "C:\Users\kdaftari\Code\barepos\dashboards\cookie_dash.py", line 9, in get_manager
    return stx.CookieManager()
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\extra_streamlit_components\CookieManager\__init__.py", line 19, in __init__
    self.cookies = self.cookie_manager(method="getAll", key=key, default={})

error

AttributeError
Traceback:
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
File "C:\Users\kdaftari\Code\barepos\dashboards\cookie_dash.py", line 11, in <module>
    cookie_manager = get_manager()
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 178, in wrapper
    return cached_func(*args, **kwargs)
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 207, in __call__
    return self._get_or_create_cached_value(args, kwargs)
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 232, in _get_or_create_cached_value
    return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 286, in _handle_cache_miss
    computed_value = self._info.func(*func_args, **func_kwargs)
File "C:\Users\kdaftari\Code\barepos\dashboards\cookie_dash.py", line 9, in get_manager
    return stx.CookieManager()
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\extra_streamlit_components\CookieManager\__init__.py", line 19, in __init__
    self.cookies = self.cookie_manager(method="getAll", key=key, default={})
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\components\v1\components.py", line 79, in __call__
    return self.create_instance(*args, default=default, key=key, **kwargs)
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\metrics_util.py", line 311, in wrapped_func
    result = non_optional_func(*args, **kwargs)
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\components\v1\components.py", line 223, in create_instance
    result = dg._enqueue(
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\delta_generator.py", line 563, in _enqueue
    caching.save_element_message(
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\caching\__init__.py", line 49, in save_element_message
    CACHE_RESOURCE_MESSAGE_REPLAY_CTX.save_element_message(
File "C:\Users\kdaftari\Code\barepos\venv\lib\site-packages\streamlit\runtime\caching\cached_message_replay.py", line 295, in save_element_message
    raise AttributeError
2 Likes

I found this to be effective:

@st.cache_resource(experimental_allow_widgets=True)
1 Like

Thats what is recommended by the error page itself. But I wonder, if thats the right thing to do in this context. (The page says – if you know what you are doing, then use this…)

app ran fine before using streamlit cache_resource

@st.cache_resource
def min_func(weights):
return statistics(weights)[1]**2

Today got error -
"streamlit’ has no attribute cache_resource.

Q: anything changed?

Make sure your version of streamlit is recent enough.

Thank you Goyo.

Resolution - my Anaconda may not have the latest version. So I updated the app (codes with Github and Streamlit online), rebooted and problem went away.