label_documentation.py 925 B

1234567891011121314151617181920212223242526
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. ui.label('some label')
  5. def more() -> None:
  6. @text_demo('Change Appearance Depending on the Content', '''
  7. You can overwrite the `on_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 on_text_change(self, text: str) -> None:
  13. super().on_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'))