将Figure对象以虚线方式传递给Graph



当我试图将Figure对象传递给布局中的dcc.Graph()时,我会得到一个错误,上面写着:

dash.exceptions.InvalidCallbackReturnValue: The callback ..graph.figure.. is a multi-output.
Expected the output type to be a list or tuple but got: 
Figure({# the content of the figure})

我的代码是这样的:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
app.layout = html.Div([
dcc.Graph(
id='graph'
)
])
@app.callback(
[
Output('graph', 'figure'),
],
[
Input('my-input', 'value')
]
)
def gen_graph(value):
dff = # my filtered df
fig = px.line(dff, x='x_var', y='y_var')
return fig

感觉我错过了Figure应该如何传递给dcc.Graph()的一些东西。有什么想法吗?

您将Output结构化为一个列表,这使它成为一个多输出回调。就这样改吧:

@app.callback(
Output('graph', 'figure'),
[
Input('my-input', 'value')
]
)
def gen_graph(value):
...

或者,您可以将输出封装在括号中,使其成为列表(return [fig](。无论哪种方式都应该很好。

最新更新