api_router.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from pathlib import Path
  2. from typing import Callable, Optional, Union
  3. import fastapi
  4. from .page import page as ui_page
  5. class APIRouter(fastapi.APIRouter):
  6. def page(self,
  7. path: str, *,
  8. title: Optional[str] = None,
  9. viewport: Optional[str] = None,
  10. favicon: Optional[Union[str, Path]] = None,
  11. dark: Optional[bool] = ..., # type: ignore
  12. response_timeout: float = 3.0,
  13. **kwargs,
  14. ) -> Callable:
  15. """Page
  16. Creates a new page at the given route.
  17. Each user will see a new instance of the page.
  18. This means it is private to the user and not shared with others
  19. (as it is done `when placing elements outside of a page decorator <https://nicegui.io/documentation#auto-index_page>`_).
  20. :param path: route of the new page (path must start with '/')
  21. :param title: optional page title
  22. :param viewport: optional viewport meta tag content
  23. :param favicon: optional relative filepath or absolute URL to a favicon (default: `None`, NiceGUI icon will be used)
  24. :param dark: whether to use Quasar's dark mode (defaults to `dark` argument of `run` command)
  25. :param response_timeout: maximum time for the decorated function to build the page (default: 3.0)
  26. :param kwargs: additional keyword arguments passed to FastAPI's @app.get method
  27. """
  28. return ui_page(
  29. path,
  30. title=title,
  31. viewport=viewport,
  32. favicon=favicon,
  33. dark=dark,
  34. response_timeout=response_timeout,
  35. api_router=self,
  36. **kwargs
  37. )