timer_documentation.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class TimerDocumentation(UiElementDocumentation, element=ui.timer):
  4. def main_demo(self) -> None:
  5. from datetime import datetime
  6. label = ui.label()
  7. ui.timer(1.0, lambda: label.set_text(f'{datetime.now():%X}'))
  8. def more(self) -> None:
  9. @self.demo('Activate, deactivate and cancel a timer', '''
  10. You can activate and deactivate a timer using the `active` property.
  11. You can cancel a timer using the `cancel` method.
  12. After canceling a timer, it cannot be activated anymore.
  13. ''')
  14. def activate_deactivate_demo():
  15. slider = ui.slider(min=0, max=1, value=0.5)
  16. timer = ui.timer(0.1, lambda: slider.set_value((slider.value + 0.01) % 1.0))
  17. ui.switch('active').bind_value_to(timer, 'active')
  18. ui.button('Cancel', on_click=timer.cancel)
  19. @self.demo('Call a function after a delay', '''
  20. You can call a function after a delay using a timer with the `once` parameter.
  21. ''')
  22. def call_after_delay_demo():
  23. def handle_click():
  24. ui.timer(1.0, lambda: ui.notify('Hi!'), once=True)
  25. ui.button('Notify after 1 second', on_click=handle_click)