Skip to content

Commit

Permalink
Allow Gradio apps containing gr.Radio(), gr.Checkboxgroup(), or `…
Browse files Browse the repository at this point in the history
…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 <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot committed Sep 20, 2023
1 parent 38fafb9 commit 3414023
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .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()`
4 changes: 3 additions & 1 deletion gradio/components/checkboxgroup.py
Expand Up @@ -71,7 +71,9 @@ def __init__(
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 []
)
Expand Down
4 changes: 3 additions & 1 deletion gradio/components/dropdown.py
Expand Up @@ -84,7 +84,9 @@ def __init__(
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 []
)
Expand Down
4 changes: 3 additions & 1 deletion gradio/components/radio.py
Expand Up @@ -72,7 +72,9 @@ def __init__(
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 []
)
Expand Down
15 changes: 15 additions & 0 deletions test/test_components.py
Expand Up @@ -512,6 +512,11 @@ def test_component_functions(self):
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"],
Expand Down Expand Up @@ -592,6 +597,11 @@ def test_component_functions(self):
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")

Expand Down Expand Up @@ -633,6 +643,11 @@ def test_component_functions(self):
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
Expand Down

0 comments on commit 3414023

Please sign in to comment.