Dash Callback error: unhashable type: 'Figure'

Dash Callback error: unhashable type: 'Figure'

我想用破折号更新饼图:

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

不幸的是我得到了这个错误:

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'

有人可以帮助我吗?谢谢!

在回调中 return piechart.figure 的新值。如 https://plotly.com/python/creating-and-updating-figures/ 中所述,您可以传递一个包含一个 data 和一个 layout 字段的字典。在您的代码中,您使用了错误的语法,导致图形被用作键而不是值。由于该图不可散列,因此您会收到错误消息。用plotly express可以直接传图

在您的示例中,正确的实现可能如下所示:

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