Skip to content

Commit 24a5836

Browse files
aliabid94Ali Abidgradio-pr-botakx
authored
Component Server fix (#6884)
* changes * add changeset * Update gradio/routes.py Co-authored-by: Aarni Koskela <[email protected]> * changes * changes --------- Co-authored-by: Ali Abid <[email protected]> Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Aarni Koskela <[email protected]>
1 parent 53b95f8 commit 24a5836

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

.changeset/ten-chefs-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": minor
3+
---
4+
5+
feat:Component Server fix

gradio/components/file_explorer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ def postprocess(self, value: str | list[str] | None) -> FileExplorerData | None:
139139
return FileExplorerData(root=root)
140140

141141
@server
142-
def ls(self, value=None) -> list[dict[str, str]] | None:
142+
def ls(self, _=None) -> list[dict[str, str]] | None:
143143
"""
144-
Parameters:
145-
value: file path as a list of strings for each directory level relative to the root.
146144
Returns:
147145
tuple of list of files in directory, then list of folders in directory
148146
"""

gradio/routes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,12 @@ def component_server(body: ComponentServerBody):
699699
block = state[component_id]
700700
else:
701701
block = app.get_blocks().blocks[component_id]
702-
fn = getattr(block, body.fn_name)
702+
fn = getattr(block, body.fn_name, None)
703+
if fn is None or not getattr(fn, "_is_server_fn", False):
704+
raise HTTPException(
705+
status_code=status.HTTP_404_NOT_FOUND,
706+
detail="Function not found.",
707+
)
703708
return fn(body.data)
704709

705710
@app.get(

test/test_routes.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,34 @@ def test_show_api_true_when_is_wasm_false(self):
831831
assert (
832832
interface.show_api is True
833833
), "show_api should be True when IS_WASM is False"
834+
835+
836+
def test_component_server_endpoints(connect):
837+
here = os.path.dirname(os.path.abspath(__file__))
838+
with gr.Blocks() as demo:
839+
file_explorer = gr.FileExplorer(root=here)
840+
841+
with closing(demo) as io:
842+
app, _, _ = io.launch(prevent_thread_lock=True)
843+
client = TestClient(app)
844+
success_req = client.post(
845+
"/component_server/",
846+
json={
847+
"session_hash": "123",
848+
"component_id": file_explorer._id,
849+
"fn_name": "ls",
850+
"data": None,
851+
},
852+
)
853+
assert success_req.status_code == 200
854+
assert len(success_req.json()) > 0
855+
fail_req = client.post(
856+
"/component_server/",
857+
json={
858+
"session_hash": "123",
859+
"component_id": file_explorer._id,
860+
"fn_name": "preprocess",
861+
"data": None,
862+
},
863+
)
864+
assert fail_req.status_code == 404

0 commit comments

Comments
 (0)