main.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. from nicegui import ui
  3. from datetime import datetime
  4. from matplotlib import pyplot as plt
  5. import numpy as np
  6. with ui.row():
  7. with ui.card():
  8. ui.label('Interactive elements', 'h5')
  9. with ui.row():
  10. with ui.column():
  11. ui.button('Click me!', icon='touch_app', design='outline rounded',
  12. on_click=lambda: output.set_text('Click'))
  13. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  14. ui.switch('Switch me!', on_change=lambda e: output.set_text('Switched' if e.value else 'Unswitched'))
  15. ui.slider(0, 100, on_change=lambda e: output.set_text(e.value))
  16. ui.input('Text input', on_change=lambda e: output.set_text(e.value))
  17. ui.number('Number input', on_change=lambda e: output.set_text(e.value), value=3.1415927, decimals=2)
  18. with ui.column():
  19. ui.radio(['A', 'B', 'C'], on_change=lambda e: output.set_text(e.value))
  20. ui.select(['1', '2', '3'], on_change=lambda e: output.set_text(e.value))
  21. with ui.row():
  22. ui.label('Output:')
  23. output = ui.label()
  24. with ui.column():
  25. with ui.card():
  26. ui.label('Timer', 'h5')
  27. with ui.row():
  28. ui.icon('far fa-clock')
  29. clock = ui.label()
  30. ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  31. with ui.card():
  32. ui.label('Style', 'h5')
  33. ui.icon('fas fa-umbrella-beach', size='88px', color='amber-14')
  34. ui.link('color palette', 'https://quasar.dev/style/color-palette')
  35. with ui.card():
  36. ui.label('Matplotlib', 'h5')
  37. with ui.plot(close=False) as plot:
  38. plt.title('Some plot')
  39. x, y = [], []
  40. line, = plt.plot(x, y, 'C0')
  41. def update_plot():
  42. global x, y, line
  43. with plot:
  44. x = [*x, datetime.now()][-100:]
  45. y = [*y, np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()][-100:]
  46. line.set_xdata(x)
  47. line.set_ydata(y)
  48. plt.xlim(min(x), max(x))
  49. plt.ylim(min(y), max(y))
  50. ui.timer(1.0, update_plot)
  51. with ui.card():
  52. ui.label('Line Plot', 'h5')
  53. lines = ui.line_plot(n=2, limit=20).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  54. ui.timer(1.0, lambda: lines.push([datetime.now()], [
  55. [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  56. [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  57. ]))