Search code examples
pythonplotlydropdown

insertion of list to a plotly dash dropdown resulting in empty dropdown


I have been watching various tutorials where a drop-down menu is created using dash core components. I was able to recreate the one demonstrated in youtube tutorial. The only thing I changed from that working code was that I replaced the dictionary within the options argument to a list of species that I would like the user to be able to choose from. I am okay with the label being the same as the value in the case of all the species and so I don't believe that I need the dictionary as per dash plotly dropdown documentation.

Is there a reason that I cannot insert a list in the options argument? Either in the form of a variable name or the actual list itself?

Expected outcome: List inserted to working code creates a dropdown containing all elements of the list

Actual outcome: The dropdown is created without error but is empty

Here is the code that is not producing drop-down values:

app.layout = html.Div([
    
    dcc.Dropdown(
        id = 'first-dropdown',
        options = [species],                #where species = long list of strings
        value = 'Pacific Water Shrew'       #one of the strings contained in species
    )
])

if __name__ =='__main__':
    app.run_server()

Solution

  • As per comment, if species is a list, [species] is a list of lists. Just pass the list. Also beware typos, values in your list in your comment were not consistent with value argument.

    from dash import html, dcc
    import dash
    
    # Build App
    # app = JupyterDash(__name__)
    app = dash.Dash()
    species = ['Pacific Water Shrew','Whihtebark Pine']
    app.layout = html.Div(
        [
            dcc.Dropdown(
                id="first-dropdown",
                options=species,  # where species = long list of strings
                value="Pacific Water Shrew",  # one of the strings contained in species
            ),
            html.Pre(dash.__version__)
        ]
    )
    
    app.run_server()