label_documentation.py 878 B

1234567891011121314151617181920212223242526272829
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.label)
  4. def main_demo() -> None:
  5. ui.label('some label')
  6. @doc.demo('Change Appearance Depending on the Content', '''
  7. You can overwrite the `_handle_text_change` method to update other attributes of a label depending on its content.
  8. This technique also works for bindings as shown in the example below.
  9. ''')
  10. def status():
  11. class status_label(ui.label):
  12. def _handle_text_change(self, text: str) -> None:
  13. super()._handle_text_change(text)
  14. if text == 'ok':
  15. self.classes(replace='text-positive')
  16. else:
  17. self.classes(replace='text-negative')
  18. model = {'status': 'error'}
  19. status_label().bind_text_from(model, 'status')
  20. ui.switch(on_change=lambda e: model.update(status='ok' if e.value else 'error'))
  21. doc.reference(ui.label)