mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-06 12:30:29 +08:00
allow setting initial value of gr.Dropdown
to None
to designate that no value should be initially selected (#9699)
* set initial * add changeset * fixes * revert' * add changeset * add test * fix multiselect case, add test * change --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
parent
a25a26e208
commit
ea2367ccb1
6
.changeset/sad-cows-hug.md
Normal file
6
.changeset/sad-cows-hug.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"@gradio/dropdown": minor
|
||||
"gradio": minor
|
||||
---
|
||||
|
||||
feat:allow setting initial value of `gr.Dropdown` to `None` to designate that no value should be initially selected
|
@ -16,6 +16,16 @@ if TYPE_CHECKING:
|
||||
from gradio.components import Timer
|
||||
|
||||
|
||||
class DefaultValue:
|
||||
# This sentinel is used to indicate that if the value is not explicitly set,
|
||||
# the first choice should be selected in the dropdown if multiselect is False,
|
||||
# and an empty list should be selected if multiselect is True.
|
||||
pass
|
||||
|
||||
|
||||
DEFAULT_VALUE = DefaultValue()
|
||||
|
||||
|
||||
@document()
|
||||
class Dropdown(FormComponent):
|
||||
"""
|
||||
@ -38,7 +48,13 @@ class Dropdown(FormComponent):
|
||||
choices: Sequence[str | int | float | tuple[str, str | int | float]]
|
||||
| None = None,
|
||||
*,
|
||||
value: str | int | float | Sequence[str | int | float] | Callable | None = None,
|
||||
value: str
|
||||
| int
|
||||
| float
|
||||
| Sequence[str | int | float]
|
||||
| Callable
|
||||
| DefaultValue
|
||||
| None = DEFAULT_VALUE,
|
||||
type: Literal["value", "index"] = "value",
|
||||
multiselect: bool | None = None,
|
||||
allow_custom_value: bool = False,
|
||||
@ -61,29 +77,29 @@ class Dropdown(FormComponent):
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
choices: A list of string options to choose from. An option can also be a tuple of the form (name, value), where name is the displayed name of the dropdown choice and value is the value to be passed to the function, or returned by the function.
|
||||
value: default value(s) selected in dropdown. If None, no value is selected by default. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
type: Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
|
||||
choices: a list of string or numeric options to choose from. An option can also be a tuple of the form (name, value), where name is the displayed name of the dropdown choice and value is the value to be passed to the function, or returned by the function.
|
||||
value: the value selected in dropdown. If `multiselect` is true, this should be list, otherwise a single string or number. By default, the first choice is initally selected. If set to None, no value is initally selected. If a callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
type: type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
|
||||
multiselect: if True, multiple choices can be selected.
|
||||
allow_custom_value: If True, allows user to enter a custom value that is not in the list of choices.
|
||||
allow_custom_value: if True, allows user to enter a custom value that is not in the list of choices.
|
||||
max_choices: maximum number of choices that can be selected. If None, no limit is enforced.
|
||||
filterable: If True, user will be able to type into the dropdown and filter the choices by typing. Can only be set to False if `allow_custom_value` is False.
|
||||
filterable: if True, user will be able to type into the dropdown and filter the choices by typing. Can only be set to False if `allow_custom_value` is False.
|
||||
label: the label for this component, displayed above the component if `show_label` is `True` and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component corresponds to.
|
||||
info: additional component description, appears below the label in smaller font. Supports markdown / HTML syntax.
|
||||
every: Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
|
||||
inputs: Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
|
||||
every: continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
|
||||
inputs: components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
|
||||
show_label: if True, will display label.
|
||||
container: If True, will place the component in a container - providing some extra padding around the border.
|
||||
container: if True, will place the component in a container - providing some extra padding around the border.
|
||||
scale: relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
|
||||
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
|
||||
interactive: if True, choices in this dropdown will be selectable; if False, selection will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
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.
|
||||
render: If False, component will not be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
|
||||
visible: if False, component will be hidden.
|
||||
elem_id: an optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
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.
|
||||
render: if False, component will not be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
|
||||
"""
|
||||
self.choices = (
|
||||
# Although we expect choices to be a list of tuples, it can be a list of tuples if the Gradio app
|
||||
# Although we expect choices to be a list of tuples, it can be a list of lists 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
|
||||
@ -96,8 +112,17 @@ class Dropdown(FormComponent):
|
||||
)
|
||||
self.type = type
|
||||
self.multiselect = multiselect
|
||||
|
||||
if value == DEFAULT_VALUE:
|
||||
if multiselect:
|
||||
value = []
|
||||
elif self.choices:
|
||||
value = self.choices[0][1]
|
||||
else:
|
||||
value = None
|
||||
if multiselect and isinstance(value, str):
|
||||
value = [value]
|
||||
|
||||
if not multiselect and max_choices is not None:
|
||||
warnings.warn(
|
||||
"The `max_choices` parameter is ignored when `multiselect` is False."
|
||||
|
@ -59,12 +59,6 @@
|
||||
old_input_text = input_text;
|
||||
}
|
||||
set_input_text();
|
||||
} else if (choices.length > 0) {
|
||||
old_selected_index = 0;
|
||||
selected_index = 0;
|
||||
[input_text, value] = choices[selected_index];
|
||||
old_value = value;
|
||||
old_input_text = input_text;
|
||||
}
|
||||
|
||||
$: {
|
||||
|
@ -22,12 +22,14 @@ class TestDropdown:
|
||||
dropdown = gr.Dropdown(choices=["a", "b"], type="index")
|
||||
assert dropdown.preprocess("a") == 0
|
||||
assert dropdown.preprocess("b") == 1
|
||||
assert dropdown.value == "a"
|
||||
with pytest.raises(gr.Error):
|
||||
dropdown.preprocess("c")
|
||||
|
||||
dropdown = gr.Dropdown(choices=["a", "b"], type="index", multiselect=True)
|
||||
assert dropdown.preprocess(["a"]) == [0]
|
||||
assert dropdown.preprocess(["a", "b"]) == [0, 1]
|
||||
assert dropdown.value == []
|
||||
with pytest.raises(gr.Error):
|
||||
dropdown.preprocess(["a", "b", "c"])
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user