Repository navigation
for ui.element -- @classmethod default_on_value_change #6447
Replies: 4 comments 2 replies
|
Thanks for the suggestion, @smahable! We'd rather not add a class-level default change handler, for a few reasons:
You can already get a single place for all changes by passing the same handler explicitly: from nicegui import ui
def handle_change(e):
if e.sender is name:
...
elif e.sender is age:
...
print(f'{e.sender.props.get("label")}: {e.value}') # e.g. log to a database
name = ui.input('Name', on_change=handle_change)
age = ui.number('Age', on_change=handle_change)
ui.run()It's one extra argument per element, but it keeps it explicit which elements are tracked. If you want to avoid even that, you can write a small factory function or subclass that registers the handler for you. For logging to a database and deriving values from each other, binding the elements to a data model (via |
|
"Many built-in elements are value elements under the hood: ui.dark_mode, ui.drawer, ui.header, ui.expansion, ui.tabs, ui.splitter, ui.stepper, ui.carousel and more, plus elements that create value elements internally. A global handler would receive all of these layout state changes too, which would quickly become surprising." i understand, this will become flooded with global router type of events. Thank you btw, I just discovered nicegui and really love it -- appreciate your innovation! keep up the great work |
|
sorry to belabor the point -- i was originally going to ask ui.button.default_on_change -- but i became greedy i guess So, do you think ui.button.default_on_change(handle_change) then i can these components tens or hundreds of times would this have downside? thank you! |
|
Claude gave code like yours: class DefaultChange:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.on_value_change(log_change)
class MyInput(DefaultChange, ui.input): pass
class MySelect(DefaultChange, ui.select): pass
class MySlider(DefaultChange, ui.slider): passThen added some nice suggestion as well for one default for all: class DefaultChange:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.on_value_change(log_change)
class MyInput(DefaultChange, ui.input): pass
class MySelect(DefaultChange, ui.select): pass
class MySlider(DefaultChange, ui.slider): passthanks again for the discussion! |
Uh oh!
There was an error while loading. Please reload this page.
I would like to develop just single change value handler -- with case statement to know which element it came from and have a single location to track every change. If logging to db or setting multiple elements values based on condition. Single handler making code simpler and cleaner.
I understand you have default_style, default_classes, default_props
If its set at generic element level -- then i dont need to set it at button level etc.
thank you
All reactions