documentation_pages.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import importlib
  2. import inspect
  3. import logging
  4. from typing import Dict
  5. from nicegui import context, ui
  6. from . import documentation
  7. from .documentation.content.overview import Overview
  8. from .documentation.model import ElementDocumentation, SectionDocumentation
  9. from .documentation.sections.text_elements import TextElementsDocumentation
  10. from .header import add_head_html, add_header
  11. from .style import section_heading, side_menu
  12. overview = Overview('/documentation')
  13. sections: Dict[str, SectionDocumentation] = {
  14. d.route.split('/')[-1]: d
  15. for d in [
  16. TextElementsDocumentation('/documentation/section_text_elements'),
  17. ]
  18. }
  19. elements: Dict[str, ElementDocumentation] = {
  20. element_documentation.route.split('/')[-1]: element_documentation
  21. for section in sections.values()
  22. for element_documentation in section.element_documentations
  23. }
  24. def create_overview() -> None:
  25. """Create the documentation overview."""
  26. documentation.render(overview)
  27. def create_page(name: str) -> None:
  28. doc = elements.get(name) or sections.get(name)
  29. if not doc:
  30. raise ValueError(f'unknown documentation page: {name}')
  31. documentation.render(doc)
  32. def create_section(name: str) -> None:
  33. """Create a documentation section."""
  34. add_head_html()
  35. with side_menu() as menu:
  36. ui.markdown('[← Overview](/documentation)').classes('bold-links')
  37. add_header(menu)
  38. ui.add_head_html('<style>html {scroll-behavior: auto;}</style>')
  39. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1250px] mx-auto'):
  40. documentation.create_section(name)
  41. async def create_more(name: str) -> None:
  42. """Create a documentation page for a "more" page."""
  43. if name in {'ag_grid', 'e_chart'}:
  44. name = name.replace('_', '') # NOTE: "AG Grid" leads to anchor name "ag_grid", but class is `ui.aggrid`
  45. module = importlib.import_module(f'website.documentation.more.{name}_documentation')
  46. more = getattr(module, 'more', None)
  47. api = getattr(ui, name, name)
  48. add_head_html()
  49. add_header()
  50. with side_menu() as menu:
  51. ui.markdown('[← Overview](/documentation)').classes('bold-links') # TODO: back to section
  52. with ui.column().classes('w-full p-8 lg:p-16 max-w-[1250px] mx-auto'):
  53. section_heading('Documentation', f'ui.*{name}*' if hasattr(ui, name) else f'*{name.replace("_", " ").title()}*')
  54. with menu:
  55. ui.markdown('**Demos**' if more else '**Demo**').classes('mt-4')
  56. documentation.element_demo(api)(getattr(module, 'main_demo'))
  57. if more:
  58. more()
  59. if inspect.isclass(api):
  60. with menu:
  61. ui.markdown('**Reference**').classes('mt-4')
  62. ui.markdown('## Reference').classes('mt-16')
  63. documentation.generate_class_doc(api)
  64. try:
  65. await context.get_client().connected()
  66. ui.run_javascript(f'document.title = "{name} • NiceGUI";')
  67. except TimeoutError:
  68. logging.warning(f'client did not connect for page /documentation/{name}')