Răsfoiți Sursa

providing NiceGUI APIRouter with page decorator

Rodja Trappe 2 ani în urmă
părinte
comite
7e96b35e58

+ 11 - 0
examples/modularization/c.py

@@ -0,0 +1,11 @@
+import theme
+
+from nicegui import APIRouter, ui
+
+router = APIRouter()
+
+
+@router.page('/c')
+def example_page():
+    with theme.frame('- Example C -'):
+        ui.label('Example C').classes('text-h4 text-grey-8')

+ 0 - 5
examples/modularization/example_pages.py

@@ -14,8 +14,3 @@ def create() -> None:
     def example_page():
         with theme.frame('- Example B -'):
             ui.label('Example B').classes('text-h4 text-grey-8')
-
-    @ui.page('/c')
-    def example_page():
-        with theme.frame('- Example C -'):
-            ui.label('Example C').classes('text-h4 text-grey-8')

+ 4 - 1
examples/modularization/main.py

@@ -1,9 +1,10 @@
 #!/usr/bin/env python3
+import c
 import example_pages
 import home_page
 import theme
 
-from nicegui import ui
+from nicegui import app, ui
 
 
 # here we use our custom page decorator directly and just put the content creation into a separate function
@@ -16,4 +17,6 @@ def index_page() -> None:
 # this call shows that you can also move the whole page creation into a separate file
 example_pages.create()
 
+app.include_router(c.router)
+
 ui.run(title='Modularization Example')

+ 1 - 0
nicegui/__init__.py

@@ -6,6 +6,7 @@ except ModuleNotFoundError:
 __version__ = importlib_metadata.version('nicegui')
 
 from . import elements, globals, ui
+from .api_router import APIRouter
 from .client import Client
 from .nicegui import app
 from .tailwind import Tailwind

+ 11 - 0
nicegui/api_router.py

@@ -0,0 +1,11 @@
+from typing import Callable
+
+import fastapi
+
+from .page import page as ui_page
+
+
+class APIRouter(fastapi.APIRouter):
+
+    def page(self, func: Callable) -> Callable:
+        return ui_page(func, api_router=self)

+ 7 - 2
nicegui/page.py

@@ -1,7 +1,7 @@
 import asyncio
 import inspect
 import time
-from typing import Callable, Optional
+from typing import TYPE_CHECKING, Callable, Optional
 
 from fastapi import Request, Response
 
@@ -9,6 +9,9 @@ from . import background_tasks, globals
 from .client import Client
 from .favicon import create_favicon_route
 
+if TYPE_CHECKING:
+    from .api_router import APIRouter
+
 
 class page:
 
@@ -19,6 +22,7 @@ class page:
                  favicon: Optional[str] = None,
                  dark: Optional[bool] = ...,
                  response_timeout: float = 3.0,
+                 api_router: Optional['APIRouter'] = None,
                  **kwargs,
                  ) -> None:
         """Page
@@ -43,6 +47,7 @@ class page:
         self.dark = dark
         self.response_timeout = response_timeout
         self.kwargs = kwargs
+        self.api_router = api_router or globals.app.router
 
         create_favicon_route(self.path, favicon)
 
@@ -89,6 +94,6 @@ class page:
             parameters.insert(0, request)
         decorated.__signature__ = inspect.Signature(parameters)
 
-        globals.app.get(self.path, **self.kwargs)(decorated)
+        self.api_router.get(self.path, **self.kwargs)(decorated)
         globals.page_routes[func] = self.path
         return func