-
Notifications
You must be signed in to change notification settings - Fork 955
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
rx.cached_var
and rx.background
don't work in mixins
#3253
Comments
Thanks for reporting, I will investigate and fix this |
@benedikt-bartscher Awesome! |
@TimChild thanks, if you have any other issues regarding mixins, feel free to ping me. |
@benedikt-bartscher Well, while it is fresh in your mind... I do have a question/issue that is fairly related to this. Obviously, don't feel obliged to spend significant time on this, but I just wonder whether you may have a different way of looking at this problem. I am trying to figure out how get around the fact that I can't use async code within an Below is an example that illustrates the idea: class Data(BaseModel):
attr_a: str
attr_b: str
database: dict[int, Data] = {}
def load_data(data_id: int) -> Data:
# Stand in for an async load from db function
return database.get(data_id, Data(attr_a='', attr_b=''))
def process(input_a: str):
# Stand in for an async processing function that stores result in db and returns only the id
data = Data(attr_a=input_a.title(), attr_b=input_a.upper())
next_id = len(database)
database[next_id] = data
return next_id
class HandlerState(rx.State):
input_a: str
data_id: int
async def do_stuff_on_click(self):
# Some process that stores result in redis/database
data_id = process(self.input_a)
# Only store the id here to avoid storing whole object in state and serialization issues
self.data_id = data_id
class DisplayMixin(rx.Base):
@rx.cached_var
def data_a_cached(self) -> str:
data = load_data(self.data_id)
return data.attr_a
@rx.cached_var
def data_b_cached(self) -> str:
data = load_data(self.data_id)
return data.attr_b
class DisplayState(DisplayMixin, HandlerState):
pass
class AlternativeDisplayMixin(rx.Base):
data_attr_a: str = ""
data_attr_b: str = ""
async def update_display_info(self):
data = load_data(self.data_id)
self.data_attr_a = f'Attribute A: {data.attr_a}'
self.data_attr_b = f'Attribute B: {data.attr_b}'
class AlternativeDisplayState(AlternativeDisplayMixin, HandlerState):
pass
@template(route='/async_cached_var_issues', title='Async Cached Var Issues')
def index() -> rx.Component:
return rx.container(
rx.card(
rx.heading('Handler stuff', size='5'),
rx.input(value=HandlerState.input_a, label='Input A', on_change=HandlerState.set_input_a),
rx.button('Do Stuff', on_click=HandlerState.do_stuff_on_click),
),
rx.hstack(
rx.card(
rx.heading('Display data via cached_vars', size='5'),
rx.markdown(f'{DisplayState.data_a_cached}\n\n{DisplayState.data_b_cached}'),
),
rx.card(
rx.heading('Alternative Display of data', size='5'),
rx.markdown(f'{AlternativeDisplayState.data_attr_a}\n\n{AlternativeDisplayState.data_attr_b}'),
rx.button('Update Display Info', on_click=AlternativeDisplayState.update_display_info),
)
),
) In the example code here, I have left the My thinking is that the Then displaying of the information is separated into a mixin because there are several ways I might want to display the processed data. The idea is that they can load the relevant data based on The The I feel like there must be some nice clean solution to this... but it eludes me... The |
Describe the bug
Trying to mixin an
rx.background
method causes error that "only background tasks should use async with self".Mixing in
rx.cached_var
doesn't do any caching and instead works like a normalrx.var
.To Reproduce
Steps to reproduce the behavior:
Expected behavior
On click of
increment b
orincrement c
expect that therx.cached_var
is not run. This is true if the cached var is moved to the State, but not if it lives in the mixin.On click of
increment c
expect that the value increments like a normal background task would. Instead an error is raised "TypeError: Only background task should useasync with self
to modify state.". Works as expected if moved to the State.Specifics (please complete the following information):
Additional context
If it's not easy to implement, it would be good to get some compilation errors :)
Related PR that allowed
rx.var
to work in mixins #2351The text was updated successfully, but these errors were encountered: