timer_documentation.py 1.0 KB

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