Allow None in as_example for files (#2588)

* Fix file examples

* Changelog

* Fix changelog version name previous release
This commit is contained in:
Freddy Boulton 2022-11-01 17:36:38 -04:00 committed by GitHub
parent e6cda90b69
commit f0d1cdf08f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View File

@ -4,7 +4,7 @@
No changes to highlight.
## Bug Fixes:
No changes to highlight.
* Fixed bug where None could not be used for File,Model3D, and Audio examples by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2588](https://github.com/gradio-app/gradio/pull/2588)
## Documentation Changes:
No changes to highlight.
@ -22,7 +22,7 @@ No changes to highlight.
No changes to highlight.
# Version 3.8.1de# Version 1
# Version 3.8.2
## Bug Fixes:

View File

@ -2023,7 +2023,7 @@ class Audio(
)
def as_example(self, input_data: str) -> str:
return Path(input_data).name
return Path(input_data).name if input_data else ""
@document("change", "clear", "style")
@ -2202,8 +2202,10 @@ class File(Changeable, Clearable, Uploadable, IOComponent, FileSerializable):
**kwargs,
)
def as_example(self, input_data: str | List) -> str:
if isinstance(input_data, list):
def as_example(self, input_data: str | List | None) -> str | List[str]:
if input_data is None:
return ""
elif isinstance(input_data, list):
return [Path(file).name for file in input_data]
else:
return Path(input_data).name
@ -3615,7 +3617,7 @@ class Model3D(Changeable, Editable, Clearable, IOComponent, FileSerializable):
)
def as_example(self, input_data: str) -> str:
return Path(input_data).name
return Path(input_data).name if input_data else ""
@document("change", "clear")

View File

@ -1921,10 +1921,11 @@ def test_dataframe_as_example_converts_dataframes():
assert df_comp.as_example(np.array([[1, 2], [3, 4.0]])) == [[1.0, 2.0], [3.0, 4.0]]
@pytest.mark.parametrize("component", [gr.Model3D, gr.File])
@pytest.mark.parametrize("component", [gr.Model3D, gr.File, gr.Audio])
def test_as_example_returns_file_basename(component):
component = component()
assert component.as_example("/home/freddy/sources/example.ext") == "example.ext"
assert component.as_example(None) == ""
@patch("gradio.components.IOComponent.as_example")