From 34140233794c29d4722020e13c2d045da642dfae Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 20 Sep 2023 11:54:19 -0700 Subject: [PATCH] Allow Gradio apps containing `gr.Radio()`, `gr.Checkboxgroup()`, or `gr.Dropdown()` to be loaded with `gr.load()` (#5633) * fix dataframe height * fix * lint * add changeset * restore * add changeset * added comments for clarity * fix tests --------- Co-authored-by: gradio-pr-bot --- .changeset/great-mammals-lead.md | 5 +++++ gradio/components/checkboxgroup.py | 4 +++- gradio/components/dropdown.py | 4 +++- gradio/components/radio.py | 4 +++- test/test_components.py | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 .changeset/great-mammals-lead.md diff --git a/.changeset/great-mammals-lead.md b/.changeset/great-mammals-lead.md new file mode 100644 index 0000000000..1f87d476bd --- /dev/null +++ b/.changeset/great-mammals-lead.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Allow Gradio apps containing `gr.Radio()`, `gr.Checkboxgroup()`, or `gr.Dropdown()` to be loaded with `gr.load()` diff --git a/gradio/components/checkboxgroup.py b/gradio/components/checkboxgroup.py index 793247853e..deaa59c9a4 100644 --- a/gradio/components/checkboxgroup.py +++ b/gradio/components/checkboxgroup.py @@ -71,7 +71,9 @@ class CheckboxGroup( elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles. """ self.choices = ( - [c if isinstance(c, tuple) else (str(c), c) for c in choices] + # Although we expect choices to be a list of tuples, it can be a list of tuples if the Gradio app + # is loaded with gr.load() since Python tuples are converted to lists in JSON. + [tuple(c) if isinstance(c, (tuple, list)) else (str(c), c) for c in choices] if choices else [] ) diff --git a/gradio/components/dropdown.py b/gradio/components/dropdown.py index 022a3257c3..13fb1e6617 100644 --- a/gradio/components/dropdown.py +++ b/gradio/components/dropdown.py @@ -84,7 +84,9 @@ class Dropdown( elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles. """ self.choices = ( - [c if isinstance(c, tuple) else (str(c), c) for c in choices] + # Although we expect choices to be a list of tuples, it can be a list of tuples if the Gradio app + # is loaded with gr.load() since Python tuples are converted to lists in JSON. + [tuple(c) if isinstance(c, (tuple, list)) else (str(c), c) for c in choices] if choices else [] ) diff --git a/gradio/components/radio.py b/gradio/components/radio.py index 2b11cb7de7..2e1d1aad80 100644 --- a/gradio/components/radio.py +++ b/gradio/components/radio.py @@ -72,7 +72,9 @@ class Radio( elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles. """ self.choices = ( - [c if isinstance(c, tuple) else (str(c), c) for c in choices] + # Although we expect choices to be a list of tuples, it can be a list of tuples if the Gradio app + # is loaded with gr.load() since Python tuples are converted to lists in JSON. + [tuple(c) if isinstance(c, (tuple, list)) else (str(c), c) for c in choices] if choices else [] ) diff --git a/test/test_components.py b/test/test_components.py index 4001dcd63f..20b53af4b5 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -512,6 +512,11 @@ class TestCheckboxGroup: assert checkboxes_input.preprocess(["a", "b"]) == [0, 1] assert checkboxes_input.preprocess(["a", "b", "c"]) == [0, 1, None] + # When a Gradio app is loaded with gr.load, the tuples are converted to lists, + # so we need to test that case as well + checkboxgroup = gr.CheckboxGroup(["a", "b", ["c", "c full"]]) # type: ignore + assert checkboxgroup.choices == [("a", "a"), ("b", "b"), ("c", "c full")] + checkboxes_input = gr.CheckboxGroup( value=["a", "c"], choices=["a", "b", "c"], @@ -592,6 +597,11 @@ class TestRadio: assert radio.preprocess("b") == 1 assert radio.preprocess("c") is None + # When a Gradio app is loaded with gr.load, the tuples are converted to lists, + # so we need to test that case as well + radio = gr.Radio(["a", "b", ["c", "c full"]]) # type: ignore + assert radio.choices == [("a", "a"), ("b", "b"), ("c", "c full")] + with pytest.raises(ValueError): gr.Radio(["a", "b"], type="unknown") @@ -633,6 +643,11 @@ class TestDropdown: assert dropdown_input.preprocess("c full") == "c full" assert dropdown_input.postprocess("c full") == "c full" + # When a Gradio app is loaded with gr.load, the tuples are converted to lists, + # so we need to test that case as well + dropdown_input = gr.Dropdown(["a", "b", ["c", "c full"]]) # type: ignore + assert dropdown_input.choices == [("a", "a"), ("b", "b"), ("c", "c full")] + dropdown = gr.Dropdown(choices=["a", "b"], type="index") assert dropdown.preprocess("a") == 0 assert dropdown.preprocess("b") == 1