Przeglądaj źródła

re-activate first page examples

Falko Schindler 2 lat temu
rodzic
commit
8473478e22
4 zmienionych plików z 27 dodań i 8 usunięć
  1. 4 4
      api_docs_and_examples.py
  2. 2 1
      nicegui/elements/link.py
  3. 3 1
      nicegui/globals.py
  4. 18 2
      nicegui/page.py

+ 4 - 4
api_docs_and_examples.py

@@ -644,17 +644,17 @@ Note: You can also pass a `functools.partial` into the `on_click` property to wr
 
     h3('Pages')
 
-    @example(ui.page)
+    @example(ui.page, skip=False)
     def page_example():
         @ui.page('/other_page')
         def other_page():
             ui.label('Welcome to the other side')
-            ui.link('Back to main page', '#page')
+            ui.link('Back to main page', '/#page')
 
         @ui.page('/dark_page', dark=True)
         def dark_page():
             ui.label('Welcome to the dark side')
-            ui.link('Back to main page', '#page')
+            ui.link('Back to main page', '/#page')
 
         ui.link('Visit other page', other_page)
         ui.link('Visit dark page', dark_page)
@@ -693,7 +693,7 @@ To make it "private" or to change other attributes like title, favicon etc. you
 Page routes can contain parameters like [FastAPI](https://fastapi.tiangolo.com/tutorial/path-params/>).
 If type-annotated, they are automatically converted to bool, int, float and complex values.
 If the page function expects a `request` argument, the request object is automatically provided.
-''')
+''', skip=False)
     def page_with_path_parameters_example():
         @ui.page('/repeat/{word}/{count}')
         def page(word: str, count: int):

+ 2 - 1
nicegui/elements/link.py

@@ -1,5 +1,6 @@
 from typing import Callable, Union
 
+from .. import globals
 from ..element import Element
 from .mixins.text_element import TextElement
 
@@ -18,7 +19,7 @@ class Link(TextElement):
         :param target: page function or string that is a an absolute URL or relative path from base URL
         """
         super().__init__(tag='a', text=text)
-        self._props['href'] = target if isinstance(target, str) else None  # TODO: globals.find_route(target)[1:]
+        self._props['href'] = target if isinstance(target, str) else globals.page_routes[target]
         self._classes.extend(['underline, text-blue'])
 
 

+ 3 - 1
nicegui/globals.py

@@ -1,6 +1,6 @@
 import asyncio
 import logging
-from typing import TYPE_CHECKING, Dict, List, Optional
+from typing import TYPE_CHECKING, Callable, Dict, List, Optional
 
 from fastapi import FastAPI
 from socketio import AsyncServer
@@ -20,3 +20,5 @@ binding_refresh_interval: float
 client_stack: List['Client'] = []
 clients: Dict[int, 'Client'] = {}
 next_client_id: int = 0
+
+page_routes: Dict[Callable, str] = {}

+ 18 - 2
nicegui/page.py

@@ -1,7 +1,7 @@
 import asyncio
 import inspect
 import time
-from typing import Callable
+from typing import Callable, Optional
 
 from fastapi import Response
 
@@ -12,9 +12,23 @@ from .task_logger import create_task
 
 class page:
 
-    def __init__(self, path: str, response_timeout: float = 3.0) -> None:
+    def __init__(self,
+                 path: str, *,
+                 dark: Optional[bool] = ...,
+                 response_timeout: float = 3.0,
+                 ) -> None:
+        """Page
+
+        Creates a new page at the given route.
+
+        :param path: route of the new page (path must start with '/')
+        :param dark: whether to use Quasar's dark mode (defaults to `dark` argument of `run` command)
+        :param response_timeout: maximum time for the decorated function to build the page (default: 3.0)
+        """
         self.path = path
+        self.dark = dark  # TODO: actually use this value
         self.response_timeout = response_timeout
+
         # NOTE we need to remove existing routes for this path to make sure only the latest definition is used
         globals.app.routes[:] = [r for r in globals.app.routes if r.path != path]
 
@@ -45,4 +59,6 @@ class page:
         parameters = [p for p in inspect.signature(func).parameters.values() if p.name != 'client']
         decorated.__signature__ = inspect.Signature(parameters)
 
+        globals.page_routes[decorated] = self.path
+
         return globals.app.get(self.path)(decorated)