Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended use of new cache features in regards to CookieManager #40

Closed
ksdaftari opened this issue Mar 7, 2023 · 12 comments
Closed

Comments

@ksdaftari
Copy link

what is recommended change for 1.18+ of streamlit for below code snippet (given st.cache is now deprecated)?

@st.cache(allow_output_mutation=True)
def get_manager():
    return stx.CookieManager()
@ksdaftari
Copy link
Author

ksdaftari commented Mar 7, 2023

I am getting below warning if just used the code snippet from readme switching st.cache_resource

warning from streamlit

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={})

the error traceback after warning

ttributeError
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

@VovaViliLox
Copy link

Neither cache_data nor cache_resources have a flag that allows output mutation, so I am confused as well.

@aht
Copy link

aht commented Mar 17, 2023

+1 Currently Streamlit shows a deprecation warning but neither cache_data nor cache_resource can be used.

@moonbug-car
Copy link

+1 it is unusable in streamlit 1.18+

@callzhang
Copy link

+1 need a way to use st.cached_data

@Mohamed-512
Copy link
Owner

New package version uses st.cache_resource

@WillReynolds5
Copy link

+1

@hoorelbeke-jimmy
Copy link

I still have the same issue with the following snippet:

import extra_streamlit_components as stx
import streamlit as st


@st.cache_resource
def get_manager():
    return stx.CookieManager()


cookie_manager = get_manager()

and following versions:

$ pip list | grep streamlit
extra-streamlit-components            0.1.60
streamlit                             1.29.0

@felimuno
Copy link

same issue streamlit 1.31.1

@p50000
Copy link

p50000 commented Apr 23, 2024

+1

@thiamfook
Copy link

Same issue
streamlit 1.32.0
extra-streamlit-components 0.1.71

My temporary workaround:

@st.cache_resource(experimental_allow_widgets=True) 
def get_manager():
    return stx.CookieManager()

@pinkponk
Copy link

pinkponk commented Sep 8, 2024

extra-streamlit-components 0.1.71
streamlit 1.37.1

Get this warning when running with @thiamfook fix. I have newer streamlit version than him.

The experimental_allow_widgets parameter is deprecated and will be removed in a future release. Please remove the experimental_allow_widgets parameter from the @st.cache_resource decorator and move all widget commands outside of cached functions.

To speed up your app, we recommend moving your widgets into fragments. Find out more about fragments in [our docs](https://docs.streamlit.io/develop/api-reference/execution-flow/st.fragment).

If you have a specific use-case that requires the experimental_allow_widgets functionality, please tell us via an [issue on Github](https://github.com/streamlit/streamlit/issues).

How should one use the cookie manager?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests