Skip to content

Commit

Permalink
Allow displaying gr.Code examples by their filename if value is a tup…
Browse files Browse the repository at this point in the history
…le (#7912)

* Add code

* add changeset

* add changeset

* Only display filename

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
freddyaboulton and gradio-pr-bot committed Apr 4, 2024
1 parent 867ff16 commit a4782f7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .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
6 changes: 6 additions & 0 deletions gradio/components/code.py
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from pathlib import Path
from typing import Any, Callable, Literal

from gradio_client.documentation import document
Expand Down Expand Up @@ -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)
9 changes: 9 additions & 0 deletions test/test_components.py
Expand Up @@ -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):
Expand Down

0 comments on commit a4782f7

Please sign in to comment.