main.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. from nicegui 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!', 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(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value))
  15. ui.input(label='Text', value='abc', on_change=lambda e: output.set_text(e.value))
  16. ui.number(label='Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value))
  17. with ui.column():
  18. with ui.row():
  19. ui.radio(options=['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value))
  20. ui.radio(options={1: 'o', 2: 'oo', 3: 'ooo'}, value=1, on_change=lambda e: output.set_text(e.value))
  21. with ui.row():
  22. ui.select(options=['a', 'b', 'c'], value='a', on_change=lambda e: output.set_text(e.value))
  23. ui.select(options={1: 'a', 2: 'b', 3: 'c'}, value=1, on_change=lambda e: output.set_text(e.value))
  24. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value))
  25. ui.toggle({1: 'X', 2: 'Y', 3: 'Z'}, value=1, on_change=lambda e: output.set_text(e.value))
  26. with ui.row():
  27. ui.label('Output:')
  28. output = ui.label(' ', 'bold')
  29. with ui.column():
  30. with ui.card():
  31. ui.label('Timer', 'h5')
  32. with ui.row():
  33. ui.icon('far fa-clock')
  34. clock = ui.label()
  35. ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  36. with ui.card().add_classes('items-center'):
  37. ui.label('Style', 'h5')
  38. ui.icon('fas fa-umbrella-beach', size='70px').add_classes('text-amber-14').add_style('margin: 9px')
  39. ui.link('color palette', 'https://quasar.dev/style/color-palette')
  40. ui.button(icon='touch_app', design='outline rounded')
  41. with ui.card():
  42. ui.label('Binding', 'h5')
  43. with ui.row():
  44. n1 = ui.number(value=1.2345, format='%.2f')
  45. n2 = ui.number(format='%.3f').bind('value', n1, 'value')
  46. with ui.row():
  47. c = ui.checkbox('c1')
  48. s = ui.switch('c2').bind('value', c, 'value')
  49. with ui.row():
  50. model = type('', (), {'value': 1})
  51. ui.radio({1: 'a', 2: 'b', 3: 'c'}).bind('value', model, 'value')
  52. ui.radio({1: 'A', 2: 'B', 3: 'C'}).bind('value', model, 'value')
  53. with ui.column():
  54. ui.number().bind('value', model, 'value')
  55. ui.label().bind('text', model, 'value')
  56. with ui.row().add_classes('items-center'):
  57. on = ui.icon('visibility')
  58. ui.checkbox('visible').bind('value', on, 'visible')
  59. with ui.row():
  60. dict_ = {'key': 'binding to a dictionary'}
  61. ui.input().bind('value', dict_, 'key')
  62. ui.label().bind('text', dict_, 'key').add_style('margin-top: 2em')
  63. with ui.card():
  64. ui.label('Matplotlib', 'h5')
  65. with ui.plot(close=False) as plot:
  66. plt.title('Some plot')
  67. x, y = [], []
  68. line, = plt.plot(x, y, 'C0')
  69. def update_plot():
  70. global x, y, line
  71. with plot:
  72. x = [*x, datetime.now()][-50:]
  73. y = [*y, np.sin(0.1 * datetime.now().timestamp()) + 0.02 * np.random.randn()][-50:]
  74. line.set_xdata(x)
  75. line.set_ydata(y)
  76. plt.xlim(min(x), max(x))
  77. plt.ylim(min(y), max(y))
  78. ui.timer(1.0, update_plot)
  79. with ui.card():
  80. ui.label('Line Plot', 'h5')
  81. lines = ui.line_plot(n=2, limit=200, update_every=5).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  82. ui.timer(0.1, lambda: lines.push([datetime.now()], [
  83. [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  84. [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  85. ]))