Bläddra i källkod

Add function to insert app middleware (#22)

Nikhil Rao 2 år sedan
förälder
incheckning
e636f0dd3e
2 ändrade filer med 13 tillägg och 0 borttagningar
  1. 1 0
      pynecone/__init__.py
  2. 12 0
      pynecone/app.py

+ 1 - 0
pynecone/__init__.py

@@ -9,6 +9,7 @@ from .components import *
 from .config import Config
 from .constants import Env
 from .event import console_log, redirect, window_alert
+from .middleware import Middleware
 from .model import Model, session
 from .state import ComputedVar as var
 from .state import State

+ 12 - 0
pynecone/app.py

@@ -162,6 +162,18 @@ class App(Base):
             if out is not None:
                 return out
 
+    def add_middleware(self, middleware: Middleware, index: Optional[int] = None):
+        """Add middleware to the app.
+
+        Args:
+            middleware: The middleware to add.
+            index: The index to add the middleware at.
+        """
+        if index is None:
+            self.middleware.append(middleware)
+        else:
+            self.middleware.insert(index, middleware)
+
     def add_page(
         self,
         component: Union[Component, ComponentCallable],