main.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import time
  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', on_click=lambda: output.set_text('Click'))
  12. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  13. ui.switch('Switch me!', on_change=lambda e: output.set_text('Switched' if e.value else 'Unswitched'))
  14. ui.slider(0, 100, on_change=lambda e: output.set_text(e.value))
  15. ui.input('Text input', on_change=lambda e: output.set_text(e.value))
  16. ui.input('Number input', on_change=lambda e: output.set_text(e.value), type='number')
  17. with ui.column():
  18. ui.radio(['A', 'B', 'C'], on_change=lambda e: output.set_text(e.value))
  19. ui.select(['1', '2', '3'], on_change=lambda e: output.set_text(e.value))
  20. with ui.row():
  21. ui.label('Output:')
  22. output = ui.label()
  23. with ui.card():
  24. ui.label('Timer', 'h5')
  25. with ui.row():
  26. ui.icon('far fa-clock')
  27. clock = ui.label()
  28. ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  29. with ui.card():
  30. ui.label('Matplotlib', 'h5')
  31. with ui.plot(close=False) as plot:
  32. plt.title('Some plot')
  33. i, x, y = 0, [], []
  34. line, = plt.plot(x, y, 'C0')
  35. plt.ion()
  36. def update_plot():
  37. global i, x, y, line
  38. with plot:
  39. i += 1
  40. x = [*x, i][-100:]
  41. y = [*y, np.sin(time.time()) + 0.02 * np.random.randn()][-100:]
  42. line.set_xdata(x)
  43. line.set_ydata(y)
  44. plt.xlim(min(x), max(x))
  45. plt.ylim(min(y), max(y))
  46. ui.timer(1.0, update_plot)
  47. ui.run()