From e2e12c372a1bfff455ef74a774233ae925b66795 Mon Sep 17 00:00:00 2001 From: Xabier de Zuazo Date: Wed, 10 May 2023 14:45:39 +0200 Subject: [PATCH] Fix isssue with running with nginx #4133 (@abidlabs work rebased to v3.23.0). --- gradio/blocks.py | 7 +++++-- gradio/routes.py | 14 ++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/gradio/blocks.py b/gradio/blocks.py index ad009cf57f01d..c13d907e38132 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -71,7 +71,6 @@ def __init__( self._skip_init_processing = _skip_init_processing self._style = {} self.parent: BlockContext | None = None - self.root = "" if render: self.render() @@ -560,6 +559,8 @@ def __init__( self.file_directories = [] + self.root_path = "" + if self.analytics_enabled: data = { "mode": self.mode, @@ -1114,7 +1115,6 @@ def get_config_file(self): "show_api": self.show_api, "is_colab": utils.colab_check(), "stylesheets": self.stylesheets, - "root": self.root, } def getLayout(block): @@ -1328,6 +1328,7 @@ def launch( quiet: bool = False, show_api: bool = True, file_directories: List[str] | None = None, + root_path: str = "", _frontend: bool = True, ) -> Tuple[FastAPI, str, str]: """ @@ -1358,6 +1359,7 @@ def launch( quiet: If True, suppresses most print statements. show_api: If True, shows the api docs in the footer of the app. Default True. If the queue is enabled, then api_open parameter of .queue() will determine if the api docs are shown, independent of the value of show_api. file_directories: List of directories that gradio is allowed to serve files from (in addition to the directory containing the gradio python file). Must be absolute paths. Warning: any files in these directories or its children are potentially accessible to all users of your app. + root_path: The root path (or "mount point") of the application, if it's not served from the root ("/") of the domain. Often used when the application is behind a reverse proxy that forwards requests to the application. For example, if the application is served at "https://example.com/myapp", the `root_path` should be set to "/myapp". Returns: app: FastAPI app object that is running the demo local_url: Locally accessible link to the demo @@ -1393,6 +1395,7 @@ def reverse(text): self.height = height self.width = width self.favicon_path = favicon_path + self.root_path = root_path if enable_queue is not None: self.enable_queue = enable_queue diff --git a/gradio/routes.py b/gradio/routes.py index 044b62445e229..d886ada42da4a 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -132,6 +132,7 @@ def configure_app(self, blocks: gradio.Blocks) -> None: self.cwd = os.getcwd() self.favicon_path = blocks.favicon_path self.tokens = {} + self.root_path = blocks.root_path def get_blocks(self) -> gradio.Blocks: if self.blocks is None: @@ -219,15 +220,17 @@ def login(form_data: OAuth2PasswordRequestForm = Depends()): def main(request: fastapi.Request, user: str = Depends(get_current_user)): mimetypes.add_type("application/javascript", ".js") blocks = app.get_blocks() + root_path = request.scope.get("root_path") if app.auth is None or not (user is None): config = app.get_blocks().config + config["root"] = root_path else: config = { "auth_required": True, "auth_message": blocks.auth_message, "is_space": app.get_blocks().is_space, - "root": app.get_blocks().root, + "root": root_path, } try: @@ -252,8 +255,12 @@ def main(request: fastapi.Request, user: str = Depends(get_current_user)): @app.get("/config/", dependencies=[Depends(login_check)]) @app.get("/config", dependencies=[Depends(login_check)]) - def get_config(): - return app.get_blocks().config + def get_config(request: fastapi.Request): + root_path = request.scope.get("root_path") + config = app.get_blocks().config + config["root"] = root_path + return config + @app.get("/static/{path:path}") def static_resource(path: str): @@ -756,7 +763,6 @@ def read_main(): # Then run `uvicorn run:app` from the terminal and navigate to http://localhost:8000/gradio. """ blocks.dev_mode = False - blocks.root = path[:-1] if path.endswith("/") else path blocks.config = blocks.get_config_file() gradio_app = App.create_app(blocks)