label_documentation.py 1.1 KB

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