Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate passing a tuple for gr.Code value #9132

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/salty-vans-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Deprecate passing a tuple for gr.Code value
2 changes: 1 addition & 1 deletion demo/code/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: code"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/code/file.css"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import os\n", "from time import sleep\n", "\n", "css_file = os.path.join(os.path.abspath(''), \"file.css\")\n", "\n", "def set_lang(language):\n", " print(language)\n", " return gr.Code(language=language)\n", "\n", "def set_lang_from_path():\n", " sleep(1)\n", " return gr.Code((css_file,), language=\"css\")\n", "\n", "def code(language, code):\n", " return gr.Code(code, language=language)\n", "\n", "io = gr.Interface(lambda x: x, \"code\", \"code\")\n", "\n", "with gr.Blocks() as demo:\n", " lang = gr.Dropdown(value=\"python\", choices=gr.Code.languages)\n", " with gr.Row():\n", " code_in = gr.Code(\n", " language=\"python\",\n", " label=\"Input\",\n", " value='def all_odd_elements(sequence):\\n \"\"\"Returns every odd element of the sequence.\"\"\"',\n", " )\n", " code_out = gr.Code(label=\"Output\")\n", " btn = gr.Button(\"Run\")\n", " btn_two = gr.Button(\"Load File\")\n", "\n", " lang.change(set_lang, inputs=lang, outputs=code_in)\n", " btn.click(code, inputs=[lang, code_in], outputs=code_out)\n", " btn_two.click(set_lang_from_path, inputs=None, outputs=code_out)\n", " io.render()\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: code"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/code/file.css"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import os\n", "from time import sleep\n", "\n", "css_file = os.path.join(os.path.abspath(''), \"file.css\")\n", "\n", "def set_lang(language):\n", " print(language)\n", " return gr.Code(language=language)\n", "\n", "def set_lang_from_path():\n", " sleep(1)\n", " return gr.Code(open(css_file).read(), language=\"css\")\n", "\n", "def code(language, code):\n", " return gr.Code(code, language=language)\n", "\n", "io = gr.Interface(lambda x: x, \"code\", \"code\")\n", "\n", "with gr.Blocks() as demo:\n", " lang = gr.Dropdown(value=\"python\", choices=gr.Code.languages)\n", " with gr.Row():\n", " code_in = gr.Code(\n", " language=\"python\",\n", " label=\"Input\",\n", " value='def all_odd_elements(sequence):\\n \"\"\"Returns every odd element of the sequence.\"\"\"',\n", " )\n", " code_out = gr.Code(label=\"Output\")\n", " btn = gr.Button(\"Run\")\n", " btn_two = gr.Button(\"Load File\")\n", "\n", " lang.change(set_lang, inputs=lang, outputs=code_in)\n", " btn.click(code, inputs=[lang, code_in], outputs=code_out)\n", " btn_two.click(set_lang_from_path, inputs=None, outputs=code_out)\n", " io.render()\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
2 changes: 1 addition & 1 deletion demo/code/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def set_lang(language):

def set_lang_from_path():
sleep(1)
return gr.Code((css_file,), language="css")
return gr.Code(open(css_file).read(), language="css")

def code(language, code):
return gr.Code(code, language=language)
Expand Down
17 changes: 5 additions & 12 deletions gradio/components/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

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

from gradio_client.documentation import document
Expand Down Expand Up @@ -59,7 +58,7 @@ class Code(Component):

def __init__(
self,
value: str | Callable | tuple[str] | None = None,
value: str | Callable | None = None,
language: Literal[
"python",
"c",
Expand Down Expand Up @@ -155,20 +154,16 @@ def preprocess(self, payload: str | None) -> str | None:
"""
return payload

def postprocess(self, value: tuple[str] | str | None) -> None | str:
def postprocess(self, value: str | None) -> None | str:
"""
Parameters:
value: Expects a `str` of code or a single-element `tuple`: (filepath,) with the `str` path to a file containing the code.
value: Expects a `str` of code.
Returns:
Returns the code as a `str`.
"""
if value is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone does pass in a tuple, can we raise an informative exception telling them this is no longer supported in gr.Code?

return None
elif isinstance(value, tuple):
with open(value[0], encoding="utf-8") as file_data:
return file_data.read()
else:
return value.strip()
return value.strip()

def api_info(self) -> dict[str, Any]:
return {"type": "string"}
Expand All @@ -179,7 +174,5 @@ 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
def process_example(self, value: str | None) -> str | None:
return super().process_example(value)
6 changes: 2 additions & 4 deletions test/components/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def fn(a):

test_file_dir = Path(__file__).parent.parent / "test_files"
path = str(test_file_dir / "test_label_json.json")
with open(path) as f:
assert code.postprocess(path) == path
assert code.postprocess((path,)) == f.read()
assert code.postprocess(path) == path

assert code.get_config() == {
"value": None,
Expand Down Expand Up @@ -56,4 +54,4 @@ def test_process_example(self):
)
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"
assert code.process_example(filename) == filename
Loading