Kanishk Jain
Kanishk Jain

Reputation: 103

How to populate a dropdown in Dash using a python list and get it's value in a variable?

I have a dropdown in Jupyter notebook filled with the values in a python list. I would like to replicate the same in Plotly-Dash.Dropdown example

Upvotes: 0

Views: 5631

Answers (1)

Kanishk Jain
Kanishk Jain

Reputation: 103

I figured it out while I was at it. Put the list in a dataframe.

df= pd.DataFrame({
'c' : list
})

Code the dropdown

dcc.Dropdown(
        id='dropdown',
        options=[
            {'label':i, 'value':i} for i in df['c'].unique()
        ],
    ),
html.Div(id='output')

Callback

@app.callback(Output('output', 'children'),
          [Input('dropdown', 'value')])
def update_output_1(value):
filtered_df = df[df['c'] == value]
return filtered_df.iloc[0]['c']

Upvotes: 3

Related Questions