From 4368686061af1bf81488de55be103eea4bab891c Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Tue, 9 May 2023 16:37:49 -0500 Subject: [PATCH 1/9] nginx-root --- gradio/routes.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gradio/routes.py b/gradio/routes.py index e275d5ae2c60b..9e5447a1ddb68 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -261,8 +261,11 @@ def api_info(serialize: bool = True): @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: 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): From fb998c6e8eb3066bc9f1b3344972ae5fc7a16404 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Tue, 9 May 2023 19:19:03 -0500 Subject: [PATCH 2/9] fix --- gradio/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/routes.py b/gradio/routes.py index 9e5447a1ddb68..c2e034f484161 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -261,7 +261,7 @@ def api_info(serialize: bool = True): @app.get("/config/", dependencies=[Depends(login_check)]) @app.get("/config", dependencies=[Depends(login_check)]) - def get_config(request: Request): + def get_config(request: fastapi.Request): root_path = request.scope.get("root_path") config = app.get_blocks().config config["root"] = root_path From ad3980fce58c32f610ddd06008608073a82eb5df Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 00:32:52 -0500 Subject: [PATCH 3/9] ability to set root path --- gradio/blocks.py | 4 ++++ gradio/routes.py | 1 + 2 files changed, 5 insertions(+) diff --git a/gradio/blocks.py b/gradio/blocks.py index e6c28a94b4b17..48c73d502b9a3 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -717,6 +717,7 @@ def __init__( self.allowed_paths = [] self.blocked_paths = [] + self.root_path = "" if self.analytics_enabled: is_custom_theme = not any( @@ -1606,6 +1607,7 @@ def launch( file_directories: list[str] | None = None, allowed_paths: list[str] | None = None, blocked_paths: list[str] | None = None, + root_path: str = "", _frontend: bool = True, ) -> tuple[FastAPI, str, str]: """ @@ -1639,6 +1641,7 @@ def launch( file_directories: This parameter has been renamed to `allowed_paths`. It will be removed in a future version. allowed_paths: List of complete filepaths or parent directories that gradio is allowed to serve (in addition to the directory containing the gradio python file). Must be absolute paths. Warning: if you provide directories, any files in these directories or their subdirectories are accessible to all users of your app. blocked_paths: List of complete filepaths or parent directories that gradio is not allowed to serve (i.e. users of your app are not allowed to access). Must be absolute paths. Warning: takes precedence over `allowed_paths` and all other directories exposed by Gradio by default. + 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 @@ -1678,6 +1681,7 @@ def reverse(text): self.width = width self.favicon_path = favicon_path self.ssl_verify = ssl_verify + 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 c2e034f484161..d401c2f2391d3 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -133,6 +133,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: From 32aa1e8183bc809ef5d65bde0b981931c94c3487 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 00:41:22 -0500 Subject: [PATCH 4/9] removed root --- gradio/routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradio/routes.py b/gradio/routes.py index d401c2f2391d3..3ed84c247e7fb 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -223,6 +223,7 @@ 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 user is not None: config = app.get_blocks().config @@ -231,7 +232,7 @@ def main(request: fastapi.Request, user: str = Depends(get_current_user)): "auth_required": True, "auth_message": blocks.auth_message, "is_space": app.get_blocks().is_space, - "root": app.get_blocks().root, + "root": root_path, } try: @@ -782,7 +783,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() blocks.validate_queue_settings() gradio_app = App.create_app(blocks) From 5f8228e6fa565f4d67dcb0aaf479877c28688bbb Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 00:41:51 -0500 Subject: [PATCH 5/9] removed root --- gradio/blocks.py | 2 -- gradio/test_data/blocks_configs.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/gradio/blocks.py b/gradio/blocks.py index 48c73d502b9a3..f890206882afe 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -93,7 +93,6 @@ def __init__( self._skip_init_processing = _skip_init_processing self._style = {} self.parent: BlockContext | None = None - self.root = "" if render: self.render() @@ -1354,7 +1353,6 @@ def get_config_file(self): "show_api": self.show_api, "is_colab": utils.colab_check(), "stylesheets": self.stylesheets, - "root": self.root, "theme": self.theme.name, } diff --git a/gradio/test_data/blocks_configs.py b/gradio/test_data/blocks_configs.py index c468d0dd7ac6c..cdcad71479b22 100644 --- a/gradio/test_data/blocks_configs.py +++ b/gradio/test_data/blocks_configs.py @@ -214,7 +214,6 @@ "https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap", "https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&display=swap", ], - "root": "", "theme": "default", "layout": { "id": 5, @@ -524,7 +523,6 @@ "https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap", "https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&display=swap", ], - "root": "", "theme": "default", "layout": { "id": 5, From 92df240afc6e47a80234b5c4166e6b38d790e712 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 00:49:24 -0500 Subject: [PATCH 6/9] fix --- gradio/routes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gradio/routes.py b/gradio/routes.py index 3ed84c247e7fb..6fc1a50c1e526 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -227,6 +227,7 @@ def main(request: fastapi.Request, user: str = Depends(get_current_user)): if app.auth is None or user is not None: config = app.get_blocks().config + config["root"] = root_path else: config = { "auth_required": True, From 3e0e355f2347fc549826269c26f98b9468174064 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 01:00:25 -0500 Subject: [PATCH 7/9] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 010a14869e9cb..94d57843a7ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## New Features: -No changes to highlight. +- Adds a `root_path` parameter to `launch()` that allows running Gradio applications on subpaths (e.g. www.example.com/app) behind a proxy, by [@abidlabs](https://github.com/abidlabs) in [PR 4133](https://github.com/gradio-app/gradio/pull/4133) ## Bug Fixes: From 4c106444d7f2af33931b00ed8babcd927915827e Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 13:30:11 -0500 Subject: [PATCH 8/9] Update gradio/routes.py Co-authored-by: aliabid94 --- gradio/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/routes.py b/gradio/routes.py index 6fc1a50c1e526..cd9af705cd6b3 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -265,7 +265,7 @@ def api_info(serialize: bool = True): @app.get("/config/", dependencies=[Depends(login_check)]) @app.get("/config", dependencies=[Depends(login_check)]) def get_config(request: fastapi.Request): - root_path = request.scope.get("root_path") + root_path = request.scope.get("root_path", "") config = app.get_blocks().config config["root"] = root_path return config From 173138600040eb07ab07dfa1f6d2ddfaa9971d19 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 10 May 2023 13:30:48 -0500 Subject: [PATCH 9/9] Update gradio/routes.py Co-authored-by: aliabid94 --- gradio/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/routes.py b/gradio/routes.py index cd9af705cd6b3..49fcc6e282a4a 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -223,7 +223,7 @@ 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") + root_path = request.scope.get("root_path", "") if app.auth is None or user is not None: config = app.get_blocks().config