Skip to content

Commit

Permalink
Component Server fix (#6884)
Browse files Browse the repository at this point in the history
* changes

* add changeset

* Update gradio/routes.py

Co-authored-by: Aarni Koskela <akx@iki.fi>

* changes

* changes

---------

Co-authored-by: Ali Abid <ubuntu@ip-172-31-25-241.us-west-2.compute.internal>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Aarni Koskela <akx@iki.fi>
  • Loading branch information
4 people committed Dec 27, 2023
1 parent 53b95f8 commit 24a5836
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-chefs-argue.md
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Component Server fix
4 changes: 1 addition & 3 deletions gradio/components/file_explorer.py
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, _=None) -> list[dict[str, str]] | None:
"""
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
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):
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
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:
file_explorer = 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": file_explorer._id,
"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": file_explorer._id,
"fn_name": "preprocess",
"data": None,
},
)
assert fail_req.status_code == 404

0 comments on commit 24a5836

Please sign in to comment.