1
0
Эх сурвалжийг харах

introduce app.remove_route()

Falko Schindler 2 жил өмнө
parent
commit
818a0b3ed0

+ 4 - 0
nicegui/app.py

@@ -59,3 +59,7 @@ class App(FastAPI):
         :param directory: folder with static files to serve under the given path
         """
         globals.app.mount(path, StaticFiles(directory=directory))
+
+    def remove_route(self, path: str) -> None:
+        """Remove routes with the given path."""
+        self.routes[:] = [r for r in self.routes if getattr(r, 'path', None) != path]

+ 1 - 2
nicegui/page.py

@@ -45,8 +45,7 @@ class page:
         return self.dark if self.dark is not ... else globals.dark
 
     def __call__(self, func: Callable) -> Callable:
-        # 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 getattr(r, 'path', None) != self.path]
+        globals.app.remove_route(self.path)  # NOTE make sure only the latest route definition is used
         parameters_of_decorated_func = list(inspect.signature(func).parameters.keys())
 
         async def decorated(*dec_args, **dec_kwargs) -> Response:

+ 2 - 2
tests/conftest.py

@@ -37,8 +37,8 @@ def selenium(selenium: webdriver.Chrome) -> webdriver.Chrome:
 
 @pytest.fixture(autouse=True)
 def reset_globals() -> Generator[None, None, None]:
-    globals.app.routes[:] = [route for route in globals.app.routes
-                             if route.path != '/' and route.path not in globals.page_routes.values()]
+    for path in ['/'] + list(globals.page_routes.values()):
+        globals.app.remove_route(path)
     importlib.reload(globals)
     globals.index_client = Client(page('/'), shared=True).__enter__()
     globals.app.get('/')(globals.index_client.build_response)