plotly / dash-core-components

OBSOLETE: now part of https://github.com/plotly/dash
https://dash.plotly.com
MIT License
271 stars 146 forks source link

Infer default values and accept different options/marks format for dcc.Dropdown, dcc.Checklist, dcc.Slider #960

Closed xhluca closed 2 years ago

xhluca commented 3 years ago

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:

In a similar fashion, for dcc.Slider we could accept alternatives for the marks:

Furthermore, the default for value could be improved: when set to None/null, it would be inferred:

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
) 
xhluca commented 3 years ago

Some more thoughts and trying to gauge interest on that (please use 👍 or 👎 )

Would pydata compatibility benefit the dash user experience? i.e.

DataTable.from_dataframe(df, id="my-id")
Slider.from_array(np.array(...))
Download.from_pillow(Image.open(...))
jackparmer commented 3 years ago

Based on Slack conversations in #dash-labs, here are the optional "shorthands" that we're going to offer for Dash Core components (dcc) and the DataTable (dt) component, illustrated through a series of "hello world" basic Dash apps. Please compare the current syntax with the optional, new shorthand syntax:

Current dcc syntax: https://gist.github.com/jackparmer/864545d0c7f7596ed6b4c9b18a3b252c

Proposed shothand dcc syntax: https://gist.github.com/jackparmer/e777629e0d2ad6b7eae0c07ccc984f75

All of these "hello world" Dash examples can be found by searching here: https://dash.gallery/python-docs-dash-snippets/ (for example, enter "Legend" to get the radio button example, etc).

alexcjohnson commented 2 years ago

Closed by https://github.com/plotly/dash/pull/1763