Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a Plotly Dash dcc.dropdown value dynamically

Im building a plotly Dash dcc.dropdown list dynamically using :

def BuildOptions(DataFrameSeries, AddAll):  
    OptionList = [{'label': i, 'value': i} for i in DataFrameSeries.unique()]
    if AddAll == 1:       
        OptionList.insert(0,{'label': 'All', 'value': 'All'})          
    return OptionList

it uses unique values in a df series, and inserts 'All' into the options list. I now want to Set the (default) value to 'All' if it exists or the [0] item in the list of K/V pairs.

    html.Div([
            dcc.Dropdown(
                id='Prov_DD',                    
                options=BuildOptions(data.TASKPROVINCE,1),
               # value=data.TASKPROVINCE[0],
               # value=dcc.Dropdown.value[0],
                 value='All'  # this works for those list that have 'All' 
                              # but I want [0] item   
                multi=True
            )],className='two columns'
            ),

Any way of setting the dcc drop down to a certain item in the options list of key value pairs by index?

like image 831
Glen Avatar asked May 14 '18 15:05

Glen


People also ask

What DCC dropdown?

dcc. Dropdown is a component for rendering a user-expandable dropdown.

Can we add multiple input components to a Dash callback function?

It is possible for a callback to insert new Dash components into a Dash app's layout. If these new components are themselves the inputs to other callback functions, then their appearance in the Dash app's layout will trigger those callback functions to be executed.

Is Plotly Dash free for commercial use?

Yes. Plotly's Dash analytics application framework is also free and open-source software, licensed under the MIT license.


1 Answers

You should define an options_array. Then use that array to populate the dropdown options and value parameters. Once the array is defined you can select a default value by index from the pre-defined array (options_array).

options_array = BuildOptions(data.TASKPROVINCE,1)
html.Div([
    dcc.Dropdown(
    id='Prov_DD',                    
    options=options_array,
    value=options_array[0],
    multi=True)
  ],
  className='two columns'
),

You won't be able to grab a value from dcc.Dropdown(options=...), because it's not actually instantiated yet.

Plus, at this time I think the only other way to pragmatically update dcc.Dropdown is by using callbacks.

Just define the array first.

like image 162
Michael Colon Avatar answered Oct 23 '22 23:10

Michael Colon