Mohamed-512 / Extra-Streamlit-Components

An all in one place, to find complex or just not available components by default on streamlit.
Apache License 2.0
466 stars 59 forks source link

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

Closed ksdaftari closed 1 year ago

ksdaftari commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

aht commented 1 year ago

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

moonbug-car commented 1 year ago

+1 it is unusable in streamlit 1.18+

callzhang commented 1 year ago

+1 need a way to use st.cached_data

Mohamed-512 commented 1 year ago

New package version uses st.cache_resource

WillReynolds5 commented 11 months ago

+1

hoorelbeke-jimmy commented 8 months ago

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 commented 6 months ago

same issue streamlit 1.31.1

p50000 commented 4 months ago

+1

thiamfook commented 1 week ago

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 commented 5 days ago

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?