main.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.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', 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.column():
  24. with ui.card():
  25. ui.label('Timer', 'h5')
  26. with ui.row():
  27. ui.icon('far fa-clock')
  28. clock = ui.label()
  29. ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  30. with ui.card():
  31. ui.label('Style', 'h5')
  32. ui.icon('fas fa-umbrella-beach', size='88px', color='amber-14')
  33. ui.link('color palette', 'https://quasar.dev/style/color-palette')
  34. with ui.card():
  35. ui.label('Matplotlib', 'h5')
  36. with ui.plot(close=False) as plot:
  37. plt.title('Some plot')
  38. x, y = [], []
  39. line, = plt.plot(x, y, 'C0')
  40. def update_plot():
  41. global x, y, line
  42. with plot:
  43. x = [*x, datetime.now()][-100:]
  44. y = [*y, np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()][-100:]
  45. line.set_xdata(x)
  46. line.set_ydata(y)
  47. plt.xlim(min(x), max(x))
  48. plt.ylim(min(y), max(y))
  49. ui.timer(1.0, update_plot)
  50. with ui.card():
  51. ui.label('Line Plot', 'h5')
  52. lines = ui.line_plot(n=2, limit=20).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  53. ui.timer(1.0, lambda: lines.push([datetime.now()], [
  54. [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  55. [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  56. ]))