-
-
Notifications
You must be signed in to change notification settings - Fork 143
Infer default values and accept different options/marks format for dcc.Dropdown, dcc.Checklist, dcc.Slider #960
Description
Would it be possible to allow components like dcc.Dropdown
and dcc.Checklist
to accept options
other than a list of dictionaries with a format like {'label': label, 'value': value}
? For example:
- A list of values would be preprocessed into to a list of
{'label': value, 'value': value}
- A dictionary of
key -> val
would be processed into a list of{'label': key, 'value': value}
.
In a similar fashion, for dcc.Slider
we could accept alternatives for the marks
:
- A list of values would be preprocessed into a mapping
{i: str(i) for i in list}
marks=True
when there's a min, max, step value specified would display a mark on every stepmarks: int = n
would display a mark at everyn
steps (so{i: str(i) for i in range(min, max, n*steps)}
)
Furthermore, the default for value
could be improved: when set to None
/null
, it would be inferred:
- in the case of dropdown, the first element in
options
- in the case of checklist, an empty list
- in the case of slider, the middle value
- in the case of range slider, it would be the entire range
- etc.
In the case of dcc.Slider
, when the min and max are left as None
/null
, they could also be assigned to the small and largest values in marks
, or 0 and 100 (and step to 1) if marks
is not specified.
Overall this is just an idea but it would make the UX much nicer on the python side. Right now to define a slider we need:
my_slider = dcc.Slider(id='slider', min=0, max=10, step=2, value=5, marks={i: str(i) for i in range(0, 11, 2)})
With inferred default and polyvalent marks:
my_slider = dcc.Slider(id='slider', min=0, max=10, step=2, marks=True)
# assuming an order:
my_slider = dcc.Slider(id='slider', 0, 10, 2, marks=True)
# equivalent to:
my_slider = dcc.Slider(id='slider', marks=range(0, 11, 2))
Similarly for a dropdown:
my_lst = [...]
my_dropdown = dcc.Dropdown(options=[{'value': x, 'label': x} for x in my_lst], value=my_lst[0])
which could simply be
my_dropdown = dcc.Dropdown(my_lst)
And in the case where we want to access a dataframe column's unique values (super frequent use case for me):
# before
my_dropdown = dcc.Dropdown(
[{'value': x, 'label': x} for x in df.my_col.unique().tolist()],
value=df.my_col.unique().tolist()[0]
)
# after
my_dropdown = dcc.Dropdown(df.my_col.unique().tolist())
# and if we add some helper to handle pandas behind the scene:
my_dropdown = dcc.Dropdown(df.my_col.unique())
Obviously sometimes a mapping would work better, but in that case a key-value pair would be more concise than writing "label" and "value" many times:
# before
dcc.Dropdown(
options=[
{'label': 'New York City', 'value': 'NYC'},
{'label': 'Montreal', 'value': 'MTL'},
{'label': 'San Francisco', 'value': 'SF'},
],
value='MTL',
clearable=False
)
# after
dcc.Dropdown(
{"New York City": "NYC", "Montreal": "MTL", "San Francisco": "SF"},
clearable=False
)