main.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 random
  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. time = ui.label()
  27. ui.timer(0.1, lambda: time.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. plt.plot(range(10), [x**2 for x in range(10)])
  33. x, y, colors, areas, scatter = [], [], [], [], None
  34. def update_plot():
  35. global x, y, colors, areas, scatter
  36. n = 20
  37. with plot:
  38. plt.title('Some plot with updates')
  39. x = [*x, 10 * random.triangular()][-n:]
  40. y = [*y, 100 * random.gauss(0.5, 0.25)][-n:]
  41. colors = [*colors, random.randint(1, 4)][-n:]
  42. areas = [*areas, random.randint(10, 30)**2][-n:]
  43. if scatter is not None:
  44. scatter.remove()
  45. scatter = plt.scatter(x, y, s=areas, c=colors, alpha=0.85)
  46. ui.timer(1.0, update_plot)
  47. ui.run()