api_reference.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. import sys
  8. from pygments import highlight
  9. from pygments.lexers import PythonLexer
  10. from pygments.formatters import HtmlFormatter
  11. wp.css = HtmlFormatter().get_style_defs('.codehilite')
  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, headline):
  28. doc = element.__init__.__doc__
  29. ui.markdown(f'### {headline}\n{doc}')
  30. describe(ui.input, 'Text Input')
  31. with example():
  32. ui.input(label='Text', on_change=lambda e: result.set_text(e.value))
  33. ui.number(label='Number', format='%.2f', on_change=lambda e: result.set_text(e.value))
  34. result = ui.label('result', typography='bold')