Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add root_url to components created by gr.render #9267

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-plants-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Add root_url to components created by gr.render
4 changes: 4 additions & 0 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,10 @@ async def process_api(
block_fn.renderable
)
output["render_config"]["render_id"] = block_fn.renderable._id
if root_path is not None:
output["render_config"] = processing_utils.add_root_url(
output["render_config"], root_path, None
)

return output

Expand Down
44 changes: 44 additions & 0 deletions test/test_blocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import copy
import io
import json
import os
import pathlib
import random
Expand All @@ -16,6 +17,7 @@
import gradio_client as grc
import numpy as np
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from gradio_client import Client, media_data
from PIL import Image
Expand Down Expand Up @@ -1813,3 +1815,45 @@ def delete_fn(v):
)
finally:
demo.close()


def test_render_when_mounted_sets_root_path_for_files():
app = FastAPI()
test_video_path = "test/test_files/video_sample.mp4"

with gr.Blocks() as demo:
text = gr.Text()
gr.Video(test_video_path)

@gr.render(inputs=text)
def show_video(data):
gr.Video(test_video_path)

app = gr.mount_gradio_app(app, demo, path="/test")

with TestClient(app) as client:
r = client.post(
"/test/queue/join",
json={
"data": [""],
"fn_index": 0,
"event_data": None,
"session_hash": "foo",
"trigger_id": None,
},
)
assert r.status_code == 200
r = client.get("/test/queue/data?session_hash=foo")
checked_component = False
for msg in r.iter_lines():
if "data" in msg:
data = json.loads(msg[5:])
if data["msg"] == "process_completed":
render_config = data["output"]["render_config"]
for component in render_config["components"]:
if "value" in component.get("props", {}):
assert component["props"]["value"]["video"][
"url"
].startswith("http://testserver/test/file=")
checked_component = True
assert checked_component
Loading