[英]Passing Figure object to Graph in plotly dash
When I try to pass a Figure
object to the dcc.Graph()
in my layout, I get en error that says:当我尝试将
Figure
object 传递到布局中的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})
my code is like:我的代码是这样的:
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
Feels like I'm missing something in how the Figure
should be passed to the dcc.Graph()
.感觉我在如何将
Figure
传递给dcc.Graph()
遗漏了一些东西。 Any ideas?有任何想法吗?
You structured your Output
as a list, that makes it a multi-output callback.您将
Output
为一个列表,这使其成为一个多输出回调。 Just change it like this:像这样改变它:
@app.callback(
Output('graph', 'figure'),
[
Input('my-input', 'value')
]
)
def gen_graph(value):
...
Alternatively, you could wrap your output in brackets to make it a list ( return [fig]
).或者,您可以将 output 括在括号中以使其成为一个列表(
return [fig]
)。 Either way should work fine.无论哪种方式都应该工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.