How to get values from dropdown with button?

I want to write a simple program. A Dropdown menu, a button and a Div is in the layout. I want to choose an option from the dropdown menu and then when I press the button the choosen value in the dropdown menu appears in the Div. My code below

import dash_bootstrap_components as dbc
from dash import Dash, html, dcc, callback, Input, Output, State

app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])

layout_app = html.Div([
    html.Div([
        dcc.Dropdown(['Choose Company', 'AAPL', 'MSFT', 'TXN', 'F', 'FB'], 'Choose Company', id='company_id'),
        dbc.Button('Generate', id='gen_button', className='me-2', n_clicks=0),
        html.Div(id='output_div')
    ])
])

@callback(
    Output('output_div', 'children'),
    Input('gen_button', 'n_clicks'),
    State('company_id', 'value')
)
def dropdown_value(value, n_clicks):
    if(n_clicks):
        return f"Value from Dropdown Menu: {value}"
    else:
        return f""

app.layout = dbc.Container(layout_app)

if __name__ == '__main__':
    app.run(debug=True)

When I run this code I get a number not a value from the dropdown menu. What do I need to change in the code?

Hey @mahmoud899 , welcome to the forums.

Just swap the function parameters in you function. The order is reversed.

2 Likes