Search code examples
pythoncallbackplotlyplotly-dashplotly-python

Callback gets activated without selection in Plotly Dash


I am creating a dashboard with Plotly Dash but I am having some trouble using the callbacks.

Firstly I don't understand completely the difference between using 'value', 'options' or 'children' and how many more options are there for the input/output calls. Ex:

@app.callback(
    Output("output", "children"),
    Input("num_exp", "value"),
)

Secondly, although the dropdown works fine after I select an option, at the beginning when running the code it already gives this error returning the 'None' selection. I don't get why this happens since I thought the callback was just activated when actually selecting something in the dropdown.

error keyerror

Any help would be useful :) Thanks!


Solution

  • Ok, your callback as shown in the error message needs to have an Input. Right now, it doesn't, so the code thinks the function parameter dd_properties is None. You then try to use that to index into a list or something, and it breaks. That's your KeyError.

    The difference between the properties (props) you use (ex. value, children, etc.) is what part of the component you are targeting. So, you could have something like this

    html.Div(
        id='my-id',
        children='This is my favorite color',
        style=dict(color='blue'),  # no, yellooooooooow
    )
    

    You can have a callback output its value to the children prop by setting up your callback with Output('my-id', 'children'), or you can have it output to the style property with Output('my-id', 'style'). The same works for Input, and what prop triggers the callback. For a dropdown, if the value prop from the component is used (Input('my-dropdown', 'value')), then the callback will run when someone makes a selection in the dropdown menu. However, if the options prop is used, then the callback runs when the list of options in the dropdown changes, perhaps as a result of another callback which output to the options prop (resulting in chained callbacks).