documentation_pages.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import importlib
  2. import inspect
  3. import logging
  4. from nicegui import context, ui
  5. from . import documentation
  6. from .header import add_head_html, add_header
  7. from .style import section_heading, side_menu
  8. def create_section(name: str) -> None:
  9. """Create a documentation section."""
  10. add_head_html()
  11. with side_menu() as menu:
  12. ui.markdown('[← Overview](/documentation)').classes('bold-links')
  13. add_header(menu)
  14. ui.add_head_html('<style>html {scroll-behavior: auto;}</style>')
  15. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1250px] mx-auto'):
  16. documentation.create_section(name)
  17. async def create_more(name: str) -> None:
  18. """Create a documentation page for a "more" page."""
  19. if name in {'ag_grid', 'e_chart'}:
  20. name = name.replace('_', '') # NOTE: "AG Grid" leads to anchor name "ag_grid", but class is `ui.aggrid`
  21. module = importlib.import_module(f'website.documentation.more.{name}_documentation')
  22. more = getattr(module, 'more', None)
  23. api = getattr(ui, name, name)
  24. add_head_html()
  25. add_header()
  26. with side_menu() as menu:
  27. ui.markdown('[← Overview](/documentation)').classes('bold-links') # TODO: back to section
  28. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1250px] mx-auto'):
  29. section_heading('Documentation', f'ui.*{name}*' if hasattr(ui, name) else f'*{name.replace("_", " ").title()}*')
  30. with menu:
  31. ui.markdown('**Demos**' if more else '**Demo**').classes('mt-4')
  32. documentation.element_demo(api)(getattr(module, 'main_demo'))
  33. if more:
  34. more()
  35. if inspect.isclass(api):
  36. with menu:
  37. ui.markdown('**Reference**').classes('mt-4')
  38. ui.markdown('## Reference').classes('mt-16')
  39. documentation.generate_class_doc(api)
  40. try:
  41. await context.get_client().connected()
  42. ui.run_javascript(f'document.title = "{name} • NiceGUI";')
  43. except TimeoutError:
  44. logging.warning(f'client did not connect for page /documentation/{name}')