main.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(label='Text', on_change=lambda e: output.set_text(e.value))
  17. ui.number(label='Number', on_change=lambda e: output.set_text(e.value), value=3.1415927, decimals=2)
  18. with ui.column():
  19. with ui.row():
  20. ui.radio(options=['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value))
  21. ui.radio(options={1: 'A', 2: 'B', 3: 'C'}, value=1, on_change=lambda e: output.set_text(e.value))
  22. with ui.row():
  23. ui.select(options=['a', 'b', 'c'], value='a', on_change=lambda e: output.set_text(e.value))
  24. ui.select(options={1: 'a', 2: 'b', 3: 'c'}, value=1, on_change=lambda e: output.set_text(e.value))
  25. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value))
  26. ui.toggle({1: 'X', 2: 'Y', 3: 'Z'}, value=1, on_change=lambda e: output.set_text(e.value))
  27. with ui.row():
  28. ui.label('Output:')
  29. output = ui.label()
  30. with ui.column():
  31. with ui.card():
  32. ui.label('Timer', 'h5')
  33. with ui.row():
  34. ui.icon('far fa-clock')
  35. clock = ui.label()
  36. ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  37. with ui.card():
  38. ui.label('Style', 'h5')
  39. ui.icon('fas fa-umbrella-beach', size='88px', color='amber-14')
  40. ui.link('color palette', 'https://quasar.dev/style/color-palette')
  41. with ui.card():
  42. ui.label('Matplotlib', 'h5')
  43. with ui.plot(close=False) as plot:
  44. plt.title('Some plot')
  45. x, y = [], []
  46. line, = plt.plot(x, y, 'C0')
  47. def update_plot():
  48. global x, y, line
  49. with plot:
  50. x = [*x, datetime.now()][-100:]
  51. y = [*y, np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()][-100:]
  52. line.set_xdata(x)
  53. line.set_ydata(y)
  54. plt.xlim(min(x), max(x))
  55. plt.ylim(min(y), max(y))
  56. ui.timer(1.0, update_plot)
  57. with ui.card():
  58. ui.label('Line Plot', 'h5')
  59. lines = ui.line_plot(n=2, limit=20).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  60. ui.timer(1.0, lambda: lines.push([datetime.now()], [
  61. [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  62. [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  63. ]))