api_reference.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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('.highlight')
  12. @contextmanager
  13. def example():
  14. callFrame = inspect.currentframe().f_back.f_back
  15. begin = callFrame.f_lineno
  16. with ui.card():
  17. with ui.row():
  18. with ui.column():
  19. yield
  20. callFrame = inspect.currentframe().f_back.f_back
  21. end = callFrame.f_lineno
  22. code = inspect.getsource(sys.modules[__name__])
  23. code = code.splitlines()[begin:end]
  24. code = '\n'.join(code)
  25. html = highlight(code, PythonLexer(), HtmlFormatter())
  26. label = ui.label()
  27. label.view.inner_html = html
  28. ic(html)
  29. with example():
  30. ui.input(label='Text', on_change=lambda e: result.set_text(e.value))
  31. ui.number(label='Number', format='%.2f', on_change=lambda e: result.set_text(e.value))
  32. result = ui.label('result', typography='bold')