main.py 2.1 KB

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