From c39f06e16b9feea97984e4822df35a99c807461c Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Fri, 18 Aug 2023 07:58:33 -0700 Subject: [PATCH] Fix `.update()` for `gr.Radio()` and `gr.CheckboxGroup()` (#5254) * fix dynamic -> interactive * fix checks * add changeset * add changeset * fix changeset * fix linting issues * Fix update choices * add test * fix' * revert * revert * Delete curly-hands-carry.md * add changeset * add changeset --------- Co-authored-by: pngwn Co-authored-by: gradio-pr-bot --- .changeset/lemon-memes-clean.md | 5 +++++ gradio/components/checkboxgroup.py | 5 +++++ gradio/components/radio.py | 5 +++++ test/test_components.py | 10 ++++++++++ 4 files changed, 25 insertions(+) create mode 100644 .changeset/lemon-memes-clean.md diff --git a/.changeset/lemon-memes-clean.md b/.changeset/lemon-memes-clean.md new file mode 100644 index 0000000000..04942eb7ac --- /dev/null +++ b/.changeset/lemon-memes-clean.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Fix `.update()` for `gr.Radio()` and `gr.CheckboxGroup()` diff --git a/gradio/components/checkboxgroup.py b/gradio/components/checkboxgroup.py index 4fe9f04cce..80a5dc31b4 100644 --- a/gradio/components/checkboxgroup.py +++ b/gradio/components/checkboxgroup.py @@ -133,6 +133,11 @@ class CheckboxGroup( interactive: bool | None = None, visible: bool | None = None, ): + choices = ( + None + if choices is None + else [c if isinstance(c, tuple) else (str(c), c) for c in choices] + ) return { "choices": choices, "label": label, diff --git a/gradio/components/radio.py b/gradio/components/radio.py index 9ae10ea6a7..927bddb318 100644 --- a/gradio/components/radio.py +++ b/gradio/components/radio.py @@ -135,6 +135,11 @@ class Radio( interactive: bool | None = None, visible: bool | None = None, ): + choices = ( + None + if choices is None + else [c if isinstance(c, tuple) else (str(c), c) for c in choices] + ) return { "choices": choices, "label": label, diff --git a/test/test_components.py b/test/test_components.py index 8a49d73cdb..bca7d92cc4 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -581,6 +581,16 @@ class TestRadio: scores = (await iface.interpret(["b"]))[0]["interpretation"] assert scores == [-2.0, None, 2.0] + def test_update(self): + update = gr.Radio.update( + choices=[("zeroth", ""), "first", "second"], label="ordinal" + ) + assert update["choices"] == [ + ("zeroth", ""), + ("first", "first"), + ("second", "second"), + ] + class TestDropdown: def test_component_functions(self):