line_plot_documentation.py 1020 B

123456789101112131415161718192021222324252627282930
  1. from nicegui import events, ui
  2. def main_demo() -> None:
  3. from datetime import datetime
  4. import numpy as np
  5. line_plot = ui.line_plot(n=2, limit=20, figsize=(3, 2), update_every=5) \
  6. .with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  7. def update_line_plot() -> None:
  8. now = datetime.now()
  9. x = now.timestamp()
  10. y1 = np.sin(x)
  11. y2 = np.cos(x)
  12. line_plot.push([now], [[y1], [y2]])
  13. line_updates = ui.timer(0.1, update_line_plot, active=False)
  14. line_checkbox = ui.checkbox('active').bind_value(line_updates, 'active')
  15. # END OF DEMO
  16. def handle_change(e: events.GenericEventArguments) -> None:
  17. def turn_off() -> None:
  18. line_checkbox.set_value(False)
  19. ui.notify('Turning off that line plot to save resources on our live demo server. 😎')
  20. line_checkbox.value = e.args
  21. if line_checkbox.value:
  22. ui.timer(10.0, turn_off, once=True)
  23. line_checkbox.on('update:model-value', handle_change)