main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(), ui.card():
  7. ui.label('Binding', 'h5')
  8. n1 = ui.number(value=0.5, decimals=2)
  9. n2 = ui.number(decimals=3).bind('value', n1, 'value')
  10. with ui.row():
  11. with ui.card():
  12. ui.label('Interactive elements', 'h5')
  13. with ui.row():
  14. with ui.column():
  15. ui.button('Click me!', icon='touch_app', design='outline rounded',
  16. on_click=lambda: output.set_text('Click'))
  17. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  18. ui.switch('Switch me!', on_change=lambda e: output.set_text('Switched' if e.value else 'Unswitched'))
  19. ui.slider(0, 100, on_change=lambda e: output.set_text(e.value))
  20. ui.input(label='Text', on_change=lambda e: output.set_text(e.value))
  21. ui.number(label='Number', on_change=lambda e: output.set_text(e.value), value=3.1415927, decimals=2)
  22. with ui.column():
  23. with ui.row():
  24. ui.radio(options=['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value))
  25. ui.radio(options={1: 'A', 2: 'B', 3: 'C'}, value=1, on_change=lambda e: output.set_text(e.value))
  26. with ui.row():
  27. ui.select(options=['a', 'b', 'c'], value='a', on_change=lambda e: output.set_text(e.value))
  28. ui.select(options={1: 'a', 2: 'b', 3: 'c'}, value=1, on_change=lambda e: output.set_text(e.value))
  29. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value))
  30. ui.toggle({1: 'X', 2: 'Y', 3: 'Z'}, value=1, on_change=lambda e: output.set_text(e.value))
  31. with ui.row():
  32. ui.label('Output:')
  33. output = ui.label()
  34. with ui.column():
  35. with ui.card():
  36. ui.label('Timer', 'h5')
  37. with ui.row():
  38. ui.icon('far fa-clock')
  39. clock = ui.label()
  40. ui.timer(0.1, lambda: clock.set_text(datetime.now().strftime("%X")))
  41. with ui.card():
  42. ui.label('Style', 'h5')
  43. ui.icon('fas fa-umbrella-beach', size='88px', color='amber-14')
  44. ui.link('color palette', 'https://quasar.dev/style/color-palette')
  45. with ui.card():
  46. ui.label('Matplotlib', 'h5')
  47. with ui.plot(close=False) as plot:
  48. plt.title('Some plot')
  49. x, y = [], []
  50. line, = plt.plot(x, y, 'C0')
  51. def update_plot():
  52. global x, y, line
  53. with plot:
  54. x = [*x, datetime.now()][-100:]
  55. y = [*y, np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()][-100:]
  56. line.set_xdata(x)
  57. line.set_ydata(y)
  58. plt.xlim(min(x), max(x))
  59. plt.ylim(min(y), max(y))
  60. ui.timer(1.0, update_plot)
  61. with ui.card():
  62. ui.label('Line Plot', 'h5')
  63. lines = ui.line_plot(n=2, limit=20).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  64. ui.timer(1.0, lambda: lines.push([datetime.now()], [
  65. [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  66. [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  67. ]))