examples.py 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. from datetime import datetime
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. from nicegui import ui
  6. with ui.row():
  7. with ui.card():
  8. ui.label('Interactive elements').classes('text-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', on_change=lambda e: output.set_text(e.value)).props('inline color=green')
  27. with ui.row():
  28. ui.label('Output:')
  29. output = ui.label('').classes('text-bold')
  30. with ui.column():
  31. with ui.card():
  32. ui.label('Timer').classes('text-h5')
  33. with ui.row():
  34. ui.icon('far fa-clock')
  35. clock = ui.label()
  36. t = ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  37. ui.checkbox('active').bind_value(t, 'active')
  38. with ui.card().classes('items-center'):
  39. ui.label('Style').classes('text-h5')
  40. ui.icon('fas fa-umbrella-beach').props('size=70px').classes('text-amber-14').style('margin: 9px')
  41. ui.link('color palette', 'https://quasar.dev/style/color-palette')
  42. ui.button().props('icon=touch_app outline round')
  43. with ui.card():
  44. ui.label('Binding').classes('text-h5')
  45. with ui.row():
  46. n1 = ui.number(value=1.2345, format='%.2f')
  47. n2 = ui.number(format='%.3f').bind_value(n1, 'value')
  48. with ui.row():
  49. c = ui.checkbox('c1')
  50. ui.switch('c2').bind_value(c, 'value')
  51. ui.slider(min=0, max=1, value=0.5, step=0.01).bind_value_to(c, 'value', forward=lambda f: f > 0.5)
  52. with ui.row():
  53. model = type('Model', (), {'value': 1}) # one-liner to define an object with an attribute "value"
  54. ui.radio({1: 'a', 2: 'b', 3: 'c'}).bind_value(model, 'value')
  55. ui.radio({1: 'A', 2: 'B', 3: 'C'}).bind_value(model, 'value')
  56. with ui.column():
  57. ui.number().bind_value(model, 'value')
  58. ui.slider(min=1, max=3).bind_value(model, 'value')
  59. ui.label().bind_text(model, 'value')
  60. with ui.row().classes('items-center'):
  61. v = ui.checkbox('visible', value=True)
  62. ui.icon('visibility').bind_visibility_from(v, 'value')
  63. with ui.card():
  64. ui.label('Matplotlib').classes('text-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.5 * 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(0.5, update_plot)
  79. with ui.card():
  80. ui.label('Line Plot').classes('text-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. ]))
  86. ui.run()