mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-24 10:54:04 +08:00
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 <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
parent
38fafb9e2a
commit
3414023379
5
.changeset/great-mammals-lead.md
Normal file
5
.changeset/great-mammals-lead.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
fix:Allow Gradio apps containing `gr.Radio()`, `gr.Checkboxgroup()`, or `gr.Dropdown()` to be loaded with `gr.load()`
|
@ -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 []
|
||||
)
|
||||
|
@ -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 []
|
||||
)
|
||||
|
@ -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 []
|
||||
)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user