bind_value backward callback behavior #4101
Replies: 2 comments 2 replies
-
You should use bindable properties. See https://nicegui.io/documentation/section_binding_properties#bindable_properties_for_maximum_performance |
Beta Was this translation helpful? Give feedback.
-
So after some digging through the nicegui source, I found the reason the spamming of the backward callback even when using BindableProperty. class BindableProperty:
def __init__(self, on_change: Optional[Callable[..., Any]] = None) -> None:
self._change_handler = on_change
def __set_name__(self, _, name: str) -> None:
self.name = name # pylint: disable=attribute-defined-outside-init
def __get__(self, owner: Any, _=None) -> Any:
return getattr(owner, '___' + self.name)
def __set__(self, owner: Any, value: Any) -> None:
has_attr = hasattr(owner, '___' + self.name)
value_changed = has_attr and getattr(owner, '___' + self.name) != value
if has_attr and not value_changed:
return
setattr(owner, '___' + self.name, value)
bindable_properties[(id(owner), self.name)] = owner
_propagate(owner, self.name)
if value_changed and self._change_handler is not None:
self._change_handler(owner, value) This happens even if you have initialized all of the variables before you bind them. If someone had a large collection of data that they bound in this method, it would bog down pretty quickly, but would not be very apparent why it is happening. Perhaps this should be noted in the docs to warn to set the value immediately after binding. Or, you could do that in the bind call like so, with the line: def bind_to(self_obj: Any, self_name: str, other_obj: Any, other_name: str, forward: Callable[[Any], Any]) -> None:
...
_set_attribute(self_obj, self_name, _get_attribute(self_obj, self_name))
bindings[(id(self_obj), self_name)].append((self_obj, other_obj, other_name, forward))
if (id(self_obj), self_name) not in bindable_properties:
active_links.append((self_obj, self_name, other_obj, other_name, forward))
_propagate(self_obj, self_name) I have opened a pull request if the maintainers are interested. #4114 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Question
I am trying to use the backward callback in an input element to change how the data in a variable is displayed. I am having a few issues with it. the first issue is that the callback seems to get called endlessly, even though I have not changed the bound value.
here is a minimal example:
Edit: It also does the same thing with BindableProperty. ex:
on my system it prints 'test' endlessly. This seems like it shouldn't be happening. That seems like a lot of resources constantly being used that would bog down the backend. It should be a one and done on refresh or bound value change.
The other issue is that if the backward callback changes the value in the element, that triggers a forward callback quite a number of times. And I am assuming that the opposite happens, although I can't tell as per the first issue, I just have a constant stream of backwards callbacks no matter what.
To me I would assume that the behavior of these callbacks are such that they only trigger in one direction. So, if the bound value changes, backward should get called once to be applied to the displayed value in the element. And if the element is changed by the user, forward should be called to modify the bound value. Both things should not happen at once, right? Again, this seems like it just causes a huge wast of resources. If you need to anything remotely computational in that callback, having both side get called numerous times seems wasteful and unnecessary
Beta Was this translation helpful? Give feedback.
All reactions