Search code examples
pythondashboardplotly-dash

Dash Callback error: unhashable type: 'Figure'


I want to update a piechart with dash:

@app.callback(
    Output('piechart','figure'),
    [Input('piechartinput', 'value')]
)
def update_piechart(new_val):
    return {px.pie(dfs, names = new_val, values='Wert')}

Unfortunately I get this error:

Traceback (most recent call last):
  File "C:\Users\TO3THY0\.spyder-py3\Dashboard\Dashboard.py", line 153, in update_piechart
    return {px.pie(dfs, names = new_val, values='Wert')}
TypeError: unhashable type: 'Figure'

Can anybody help me? Thanks!


Solution

  • In your Callback you return the new value for piechart.figure. As described in https://plotly.com/python/creating-and-updating-figures/ you can pass a dictionary with one data and one layout field. In your code you are using a wrong syntax resulting in the figure being used as a key instead of the value. As the figure is not hashable you get an error. With plotly express you can pass the figure directly.

    In your example a correct implementation could look like this:

    @app.callback(
        Output('piechart','figure'),
        [Input('piechartinput', 'value')]
    )
    def update_piechart(new_val):
        return px.pie(dfs, names=new_val, values='Wert')