diff --git a/.changeset/fast-walls-drop.md b/.changeset/fast-walls-drop.md new file mode 100644 index 0000000000000..d0a885c910845 --- /dev/null +++ b/.changeset/fast-walls-drop.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +feat:Allow displaying gr.Code examples by their filename if value is a tuple diff --git a/gradio/components/code.py b/gradio/components/code.py index ed5209decb990..ad420bf1c6646 100644 --- a/gradio/components/code.py +++ b/gradio/components/code.py @@ -2,6 +2,7 @@ from __future__ import annotations +from pathlib import Path from typing import Any, Callable, Literal from gradio_client.documentation import document @@ -163,3 +164,8 @@ def example_payload(self) -> Any: def example_value(self) -> Any: return "print('Hello World')" + + def process_example(self, value: str | tuple[str] | None) -> str | None: + if isinstance(value, tuple): + return Path(value[0]).name + return super().process_example(value) diff --git a/test/test_components.py b/test/test_components.py index a29b2b27de479..9aed361aab852 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -2935,6 +2935,15 @@ def fn(a): "_selectable": False, } + def test_process_example(self): + code = gr.Code() + assert ( + code.process_example("def fn(a):\n return a") == "def fn(a):\n return a" + ) + assert code.process_example(None) is None + filename = str(Path("test/test_files/test_label_json.json")) + assert code.process_example((filename,)) == "test_label_json.json" + class TestFileExplorer: def test_component_functions(self):