main.py 4.6 KB

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