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

Component Server fix #6884

Merged
merged 5 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/ten-chefs-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Component Server fix
4 changes: 1 addition & 3 deletions gradio/components/file_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ def postprocess(self, value: str | list[str] | None) -> FileExplorerData | None:
return FileExplorerData(root=root)

@server
def ls(self, value=None) -> list[dict[str, str]] | None:
def ls(self, _) -> list[dict[str, str]] | None:
aliabid94 marked this conversation as resolved.
Show resolved Hide resolved
"""
Parameters:
value: file path as a list of strings for each directory level relative to the root.
Returns:
tuple of list of files in directory, then list of folders in directory
"""
Expand Down
7 changes: 6 additions & 1 deletion gradio/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,12 @@ def component_server(body: ComponentServerBody):
block = state[component_id]
else:
block = app.get_blocks().blocks[component_id]
fn = getattr(block, body.fn_name)
fn = getattr(block, body.fn_name, None)
if fn is None or not getattr(fn, "_is_server_fn", False):
aliabid94 marked this conversation as resolved.
Show resolved Hide resolved
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Function not found.",
)
return fn(body.data)

@app.get(
Expand Down
31 changes: 31 additions & 0 deletions test/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,3 +831,34 @@ def test_show_api_true_when_is_wasm_false(self):
assert (
interface.show_api is True
), "show_api should be True when IS_WASM is False"


def test_component_server_endpoints(connect):
here = os.path.dirname(os.path.abspath(__file__))
with gr.Blocks() as demo:
gr.FileExplorer(root=here)

with closing(demo) as io:
app, _, _ = io.launch(prevent_thread_lock=True)
client = TestClient(app)
success_req = client.post(
"/component_server/",
json={
"session_hash": "123",
"component_id": 1,
"fn_name": "ls",
"data": None,
},
)
assert success_req.status_code == 200
assert len(success_req.json()) > 0
fail_req = client.post(
"/component_server/",
json={
"session_hash": "123",
"component_id": 1,
"fn_name": "preprocess",
"data": None,
},
)
assert fail_req.status_code == 404
Loading