Using dynamic dropdown result as parameter in function

Hello - new dash user here so hopefully forgive the (probably) easy question.

I am setting up a dynamic dropdown box based on options from a dataframe - I am looking to use the result from that dropdown and apply it to a previously defined function which would then produce the function’s result right below that dropdown menu.

Here is what I have:

import dash
from dash.exceptions import PreventUpdate
import dash_html_components as html
import dash_core_components as dcc

external_stylesheets = [""]

options = [
    {"label": "New York City", "value": "NYC"},
    {"label": "Montreal", "value": "MTL"},
    {"label": "San Francisco", "value": "SF"},

def some_function(label)
    print(label + " is a city")

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
        html.Label(["Single dynamic Dropdown", dcc.Dropdown(id="my-dynamic-dropdown")])
       html.Div([some_function(id = "my-function')])

    dash.dependencies.Output("my-dynamic-dropdown", "options"),
    [dash.dependencies.Input("my-dynamic-dropdown", "search_value")],
def update_options(search_value):
    if not search_value:
        raise PreventUpdate
    return [o for o in options if search_value in o["label"]]

if __name__ == "__main__":

Looking for a dropdown that will choose a value as the user starts typing, and then take that value and apply it to the function that I have created, resulting in the printout of (for example) ‘Montreal is a city’ directly underneath the dropdown box.

Unfortunately, while I don’t get any errors, it is not showing the printout of the function once I select the option from the dropdown.

answer is in this video .
He made a dynamic a datetimepicker, but you can do it with some modification.

1 Like