Dropdown option form callback and Pandas Data Frame

A user can upload a CSV file to my DASH app. I have written a program that reads this data and saves it as a Pandas data frame.
Now I want to create a Dropdown with the options as the names of the columns of this data frame.

You can access the “options” property of the dcc.dropdown via ca callback like so:

from dash import Dash, html, dcc, Input, Output
import pandas as pd

app = Dash(__name__)

app.layout = html.Div([
    dcc.Dropdown(
        id='drop'
    ),
    html.Button(
        'click to generate dropdown options',
        id='btn'
    )
])


@app.callback(
    Output('drop', 'options'),
    Input('btn', 'n_clicks'),
    prevent_initial_call=True
)
def update(click):
    # create dummy DataFrame
    df = pd.DataFrame({f'col{i}': [1, 2, 3] for i in range(1, 5)})

    # initiate list
    options_for_dropdown = []
    for idx, colum_name in enumerate(df.columns):
        options_for_dropdown.append(
            {
                'label': colum_name,
                'value': idx
            }
        )
    return options_for_dropdown
1 Like

Thank you @AIMPED. This worked