main.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. from nicegui import ui, wp
  3. from contextlib import contextmanager
  4. import inspect
  5. from nicegui.elements.markdown import Markdown
  6. from nicegui.elements.element import Element
  7. import sys
  8. from typing import Union
  9. import docutils.core
  10. # add docutils css to webpage
  11. wp.head_html += docutils.core.publish_parts('', writer_name='html')['stylesheet']
  12. @contextmanager
  13. def example(content: Union[Element, str]):
  14. callFrame = inspect.currentframe().f_back.f_back
  15. begin = callFrame.f_lineno
  16. with ui.row(classes='flex w-full'):
  17. if isinstance(content, str):
  18. ui.markdown(content, classes='mr-8 w-4/12')
  19. else:
  20. doc = content.__init__.__doc__
  21. if doc:
  22. html = docutils.core.publish_parts(doc, writer_name='html')['html_body']
  23. html = html.replace('<p>', '<h3>', 1)
  24. html = html.replace('</p>', '</h3>', 1)
  25. html = Markdown.apply_tailwind(html)
  26. ui.html(html, classes='mr-8 w-4/12')
  27. else:
  28. ui.label(content.__name__, 'h5')
  29. with ui.card(classes='mt-12 w-2/12'):
  30. yield
  31. callFrame = inspect.currentframe().f_back.f_back
  32. end = callFrame.f_lineno
  33. code = inspect.getsource(sys.modules[__name__])
  34. code = code.splitlines()[begin:end]
  35. code = [l[4:] for l in code]
  36. code.insert(0, '```python')
  37. code.insert(1, 'from nicegui import ui')
  38. code.append('```')
  39. code = '\n'.join(code)
  40. ui.markdown(code, classes='mt-12 w-5/12 overflow-auto')
  41. with open('README.md', 'r') as file:
  42. ui.markdown(file.read())
  43. design = '''### Styling & Design
  44. NiceGUI use the [Quasar Framework](https://quasar.dev/) and hence has their full design power. Each NiceGUI element provides a `design` property which content is passed [as props the Quasar component](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components):
  45. Have a look at [the Quasar documentation](https://quasar.dev/vue-components/button#design) for all styling "props".
  46. You can also apply [Tailwind](https://tailwindcss.com/) utility classes with the `classes` property.
  47. '''
  48. with (example(design)):
  49. ui.radio(['x', 'y', 'z'], design='inline color=green')
  50. ui.button(icon='touch_app', design='outline round', classes='shadow-lg ml-14')
  51. with example(ui.timer):
  52. from datetime import datetime
  53. clock = ui.label()
  54. t = ui.timer(interval=0.1, callback=lambda: clock.set_text(datetime.now().strftime("%X")))
  55. ui.checkbox('active').bind_value(t.active)
  56. with example(ui.label):
  57. ui.label('some label')
  58. with example(ui.markdown):
  59. ui.markdown('### Headline\nWith hyperlink to [GitHub](https://github.com/zauberzeug/nicegui).')
  60. with example(ui.button):
  61. def button_increment():
  62. global button_count
  63. button_count += 1
  64. button_result.set_text(f'pressed: {button_count}')
  65. button_count = 0
  66. ui.button('Button', on_click=button_increment)
  67. button_result = ui.label('pressed: 0')
  68. with example(ui.checkbox):
  69. ui.checkbox('check me', on_change=lambda e: checkbox_state.set_text(e.value))
  70. with ui.row():
  71. ui.label('the checkbox is:')
  72. checkbox_state = ui.label('False')
  73. with example(ui.input):
  74. ui.input(
  75. label='Text',
  76. placeholder='press ENTER to apply',
  77. on_change=lambda e: result.set_text('you typed: ' + e.value)
  78. )
  79. result = ui.label('')
  80. with example(ui.plot):
  81. from matplotlib import pyplot as plt
  82. import numpy as np
  83. with ui.plot(figsize=(2.5, 1.8)):
  84. x = np.linspace(0.0, 5.0)
  85. y = np.cos(2 * np.pi * x) * np.exp(-x)
  86. plt.plot(x, y, '-')
  87. plt.xlabel('time (s)')
  88. plt.ylabel('Damped oscillation')
  89. with example(ui.line_plot):
  90. lines = ui.line_plot(n=2, limit=20, figsize=(2.5, 1.8)).with_legend(['sin', 'cos'], loc='upper center', ncol=2)
  91. line_updates = ui.timer(0.1, lambda: lines.push([datetime.now()], [
  92. [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  93. [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
  94. ]), active=False)
  95. ui.checkbox('active').bind_value(line_updates.active)