Skip to content

Commit 710bbc2

Browse files
aliabid94Ali Abidgradio-pr-botabidlabs
authored
Allow path parameter when mounting app (#11757)
* changes * add changeset * lint --------- Co-authored-by: Ali Abid <[email protected]> Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Abubakar Abid <[email protected]>
1 parent 354f236 commit 710bbc2

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

.changeset/neat-phones-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": patch
3+
---
4+
5+
fix:Allow path parameter when mounting app

gradio/routes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def configure_app(self, blocks: gradio.Blocks) -> None:
310310
self.cwd = os.getcwd()
311311
self.favicon_path = blocks.favicon_path
312312
self.tokens = {}
313-
self.root_path = blocks.root_path or blocks.custom_mount_path or ""
313+
self.root_path = blocks.root_path or ""
314314
self.state_holder.set_blocks(blocks)
315315

316316
def get_blocks(self) -> gradio.Blocks:
@@ -655,7 +655,7 @@ def main(
655655
root = route_utils.get_root_url(
656656
request=request,
657657
route_path=f"/{page}",
658-
root_path=app.root_path,
658+
root_path=app.root_path or blocks.custom_mount_path,
659659
)
660660
if (app.auth is None and app.auth_dependency is None) or user is not None:
661661
config = utils.safe_deepcopy(blocks.config)
@@ -870,7 +870,9 @@ def openapi_schema(request: fastapi.Request):
870870
def get_config(request: fastapi.Request, deep_link: str = ""):
871871
config = utils.safe_deepcopy(app.get_blocks().config)
872872
root = route_utils.get_root_url(
873-
request=request, route_path="/config", root_path=app.root_path
873+
request=request,
874+
route_path="/config",
875+
root_path=app.root_path or blocks.custom_mount_path,
874876
)
875877
config["username"] = get_current_user(request)
876878
if deep_link:
@@ -2354,6 +2356,7 @@ async def new_lifespan(app: FastAPI):
23542356

23552357
app.router.lifespan_context = new_lifespan # type: ignore
23562358

2359+
print("new", path)
23572360
app.mount(path, gradio_app)
23582361
return app
23592362

test/test_routes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ def test_mount_gradio_app_with_auth_and_params(self):
451451
assert demo.allowed_paths == ["test/test_files/bus.png"]
452452
assert demo.show_error
453453

454+
def test_mount_gradio_app_with_path_params(self):
455+
app = FastAPI()
456+
457+
def print_id(_, request: gr.Request):
458+
return request.path_params["id"]
459+
460+
demo = gr.Interface(print_id, "textbox", "textbox")
461+
app = gr.mount_gradio_app(
462+
app,
463+
demo,
464+
path="/project/{id}",
465+
)
466+
with TestClient(app) as client:
467+
response = client.get("/project/123")
468+
assert response.status_code == 200
469+
454470
def test_mount_gradio_app_with_lifespan(self):
455471
@asynccontextmanager
456472
async def empty_lifespan(app: FastAPI):

0 commit comments

Comments
 (0)