Allow displaying gr.Code examples by their filename if value is a tuple (#7912)

* Add code

* add changeset

* add changeset

* Only display filename

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Freddy Boulton 2024-04-03 17:38:55 -07:00 committed by GitHub
parent 867ff16cd4
commit a4782f7a09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
feat:Allow displaying gr.Code examples by their filename if value is a tuple

View File

@ -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 @@ class Code(Component):
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)

View File

@ -2935,6 +2935,15 @@ class TestCode:
"_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):