[Python] How does NiceGUI object binding (listener) work behind the scenes?

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    Here is a code example from their great documentation:

    NiceGUI label

    from nicegui import ui

    class status_label(ui.label):
    def _handle_text_change(self, text: str) -> None:
    super()._handle_text_change(text)
    if text == 'ok':
    self.classes(replace='text-positive')
    else:
    self.classes(replace='text-negative')

    model = {'status': 'error'}
    status_label().bind_text_from(model, 'status')
    ui.switch(on_change=lambda e: model.update(status='ok' if e.value else 'error'))

    ui.run()


    Why does this work? How do they make model reactive with assigning a whole new wrapped object to that name? I read their code but it is too highly advance for me to comprehend it.

    Continue reading...

Compartilhe esta Página