-
QuestionI have some UI things which I want to have user-configurable, so I would like check app.storage.user whenever the value is updated and possibly invert it. This appears to crash a background task, but otherwise works as expected. Minimal reproduction project: import typing
from nicegui import app, binding, ui
class MyElement(ui.element):
a_value = binding.BindableProperty(
on_change=lambda sender, value: typing.cast(typing.Self, sender)._handle_value_change(value))
def __init__(self):
super().__init__()
binding.bind_from(self, 'a_value',
app.storage.general, 'a_value',
backward=lambda v: app.storage.user.get('do_invert', False) == v)
def _handle_value_change(self, new_value: bool) -> None:
self.classes(replace=('bg-yellow' if new_value else 'bg-red'))
@ui.page('/')
def index():
with MyElement():
ui.switch('The value').bind_value(app.storage.general, 'a_value')
ui.switch('Invert it').bind_value(app.storage.user, 'do_invert')
ui.run(storage_secret='seeecret') |
Beta Was this translation helpful? Give feedback.
Answered by
rodja
May 19, 2024
Replies: 1 comment 3 replies
-
Yes, it seems that in backward/forward you do not have access to the app.storage. This might be related to #2520. A workaround may be archived like this: class MyElement(ui.element):
def __init__(self):
super().__init__()
self.a_value = False
binding.bind_from(self, 'a_value', app.storage.general, 'a_value', backward=self.transform)
def transform(self, new_value: bool) -> bool:
self.classes(replace=('bg-yellow' if new_value else 'bg-red'))
return new_value
@ui.page('/')
def index():
with MyElement().classes('w-[400px]'):
ui.switch('The value').bind_value(app.storage.general, 'a_value').bind_value(app.storage.user, 'do_invert')
ui.switch('Invert it', value=False).bind_value_from(app.storage.user, 'do_invert', backward=lambda x: not x) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
JohanAR
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, it seems that in backward/forward you do not have access to the app.storage. This might be related to #2520. A workaround may be archived like this: