main.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.row():
  8. with ui.card():
  9. ui.label('Interactive elements', 'h5')
  10. with ui.row():
  11. with ui.column():
  12. ui.button('Click me!', icon='touch_app', 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.input('Number input', on_change=lambda e: output.set_text(e.value), type='number')
  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. i, x, y = 0, [], []
  40. line, = plt.plot(x, y, 'C0')
  41. plt.ion()
  42. def update_plot():
  43. global i, x, y, line
  44. with plot:
  45. i += 1
  46. x = [*x, i][-100:]
  47. y = [*y, np.sin(time.time()) + 0.02 * np.random.randn()][-100:]
  48. line.set_xdata(x)
  49. line.set_ydata(y)
  50. plt.xlim(min(x), max(x))
  51. plt.ylim(min(y), max(y))
  52. ui.timer(1.0, update_plot)
  53. ui.run()