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

Wrap value in list for CheckBoxGroup. #2866

Merged
merged 11 commits into from
Dec 20, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ By [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2807](https://git
## Bug Fixes:
* Fixes loading Spaces that have components with default values by [@abidlabs](https://github.com/abidlabs) in [PR 2855](https://github.com/gradio-app/gradio/pull/2855)
* Fixes flagging when `allow_flagging="auto"` in `gr.Interface()` by [@abidlabs](https://github.com/abidlabs) in [PR 2695](https://github.com/gradio-app/gradio/pull/2695)
* Fixed bug where passing a non-list value to `gr.CheckboxGroup` would crash the entire app by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2866](https://github.com/gradio-app/gradio/pull/2866)

## Documentation Changes:
* Added a Guide on using BigQuery with Gradio's `DataFrame` and `ScatterPlot` component,
Expand Down
10 changes: 7 additions & 3 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ def get_config(self):

@staticmethod
def update(
value: Optional[List[str]] = _Keywords.NO_VALUE,
value: Optional[List[str] | str] = _Keywords.NO_VALUE,
choices: Optional[List[str]] = None,
label: Optional[str] = None,
show_label: Optional[bool] = None,
Expand Down Expand Up @@ -991,15 +991,19 @@ def preprocess(self, x: List[str]) -> List[str] | List[int]:
+ ". Please choose from: 'value', 'index'."
)

def postprocess(self, y: List[str] | None) -> List[str]:
def postprocess(self, y: List[str] | str | None) -> List[str]:
"""
Any postprocessing needed to be performed on function output.
Parameters:
y: List of selected choices
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
y: List of selected choices
y: List of selected choices. If a single choice is selected, it can be passed in as a string

Returns:
List of selected choices
"""
return [] if y is None else y
if y is None:
return []
if not isinstance(y, list):
y = [y]
return y

def set_interpret_parameters(self):
"""
Expand Down
4 changes: 4 additions & 0 deletions test/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ def test_component_functions(self):
with pytest.raises(ValueError):
gr.CheckboxGroup(["a"], type="unknown")

cbox = gr.CheckboxGroup(choices=["a", "b"], value="c")
assert cbox.get_config()["value"] == ["c"]
assert cbox.postprocess("a") == ["a"]

def test_in_interface(self):
"""
Interface, process
Expand Down