Skip to content

Commit

Permalink
Update file_explorer.py - Fixing error if nothing selected in file_co…
Browse files Browse the repository at this point in the history
…unt=single mode (return None rather) (#6607)

* Update file_explorer.py

Fixing error if nothing selected in file_count=single mode (return None rather)

* add changeset

* added unit tests

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 30, 2023
1 parent b8034a1 commit 13ace03
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/early-fans-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Update file_explorer.py - Fixing error if nothing selected in file_count=single mode (return None rather)
5 changes: 4 additions & 1 deletion gradio/components/file_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ def preprocess(self, payload: FileExplorerData | None) -> list[str] | str | None
raise ValueError(
f"Expected only one file, but {len(payload.root)} were selected."
)
return self._safe_join(payload.root[0])
elif len(payload.root) == 0:
return None
else:
return self._safe_join(payload.root[0])

return [self._safe_join(file) for file in (payload.root)]

Expand Down
41 changes: 41 additions & 0 deletions test/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import gradio as gr
from gradio import processing_utils, utils
from gradio.components.dataframe import DataframeData
from gradio.components.file_explorer import FileExplorerData
from gradio.components.video import VideoData
from gradio.data_classes import FileData

Expand Down Expand Up @@ -2647,6 +2648,46 @@ def fn(a):
}


class TestFileExplorer:
def test_component_functions(self):
"""
Preprocess, get_config
"""
file_explorer = gr.FileExplorer(file_count="single")

config = file_explorer.get_config()
assert config["glob"] == "**/*.*"
assert config["value"] is None
assert config["file_count"] == "single"
assert config["server_fns"] == ["ls"]

input_data = FileExplorerData(root=[["test/test_files/bus.png"]])
preprocessed_data = file_explorer.preprocess(input_data)
assert isinstance(preprocessed_data, str)
assert Path(preprocessed_data).name == "bus.png"

input_data = FileExplorerData(root=[])
preprocessed_data = file_explorer.preprocess(input_data)
assert preprocessed_data is None

file_explorer = gr.FileExplorer(file_count="multiple")

config = file_explorer.get_config()
assert config["glob"] == "**/*.*"
assert config["value"] is None
assert config["file_count"] == "multiple"
assert config["server_fns"] == ["ls"]

input_data = FileExplorerData(root=[["test/test_files/bus.png"]])
preprocessed_data = file_explorer.preprocess(input_data)
assert isinstance(preprocessed_data, list)
assert Path(preprocessed_data[0]).name == "bus.png"

input_data = FileExplorerData(root=[])
preprocessed_data = file_explorer.preprocess(input_data)
assert preprocessed_data == []


def test_component_class_ids():
button_id = gr.Button().component_class_id
textbox_id = gr.Textbox().component_class_id
Expand Down

0 comments on commit 13ace03

Please sign in to comment.