api_reference.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python3
  2. from nicegui import ui, wp
  3. from contextlib import contextmanager
  4. from icecream import ic
  5. import inspect
  6. from executing import Source
  7. from nicegui.elements.element import Element
  8. import sys
  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():
  14. callFrame = inspect.currentframe().f_back.f_back
  15. begin = callFrame.f_lineno
  16. with ui.row():
  17. with ui.card():
  18. yield
  19. callFrame = inspect.currentframe().f_back.f_back
  20. end = callFrame.f_lineno
  21. code = inspect.getsource(sys.modules[__name__])
  22. code = code.splitlines()[begin:end]
  23. code.insert(0, '```python')
  24. code.append('```')
  25. code = '\n'.join(code)
  26. ui.markdown(code)
  27. def describe(element: Element):
  28. doc = element.__init__.__doc__
  29. html = docutils.core.publish_parts(doc, writer_name='html')['html_body']
  30. html = html.replace('<p>', '<h3>', 1)
  31. html = html.replace('</p>', '</h3>', 1)
  32. ui.html(html)
  33. with open('README.md', 'r') as file:
  34. ui.markdown(file.read())
  35. describe(ui.input)
  36. with example():
  37. ui.input(label='Text', on_change=lambda e: result.set_text(e.value))
  38. ui.number(label='Number', format='%.2f', on_change=lambda e: result.set_text(e.value))
  39. result = ui.label('result', typography='bold')