Update file_explorer.py - Fixing error if nothing selected in file_count=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>
This commit is contained in:
v-chabaux 2023-11-30 09:33:10 +01:00 committed by GitHub
parent b8034a1e72
commit 13ace035ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Update file_explorer.py - Fixing error if nothing selected in file_count=single mode (return None rather)

View File

@ -112,7 +112,10 @@ class FileExplorer(Component):
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)]

View File

@ -31,6 +31,7 @@ except ImportError:
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
@ -2647,6 +2648,46 @@ class TestCode:
}
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