在 plotly dash 中将 Figure 对象传递给 Graph
Passing Figure object to Graph in plotly dash
当我尝试将 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
构造为列表,这使其成为 multi-output 回调。只需像这样更改它:
@app.callback(
Output('graph', 'figure'),
[
Input('my-input', 'value')
]
)
def gen_graph(value):
...
或者,您可以将输出括在方括号中以使其成为列表 (return [fig]
)。无论哪种方式都应该可以正常工作。
当我尝试将 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
构造为列表,这使其成为 multi-output 回调。只需像这样更改它:
@app.callback(
Output('graph', 'figure'),
[
Input('my-input', 'value')
]
)
def gen_graph(value):
...
或者,您可以将输出括在方括号中以使其成为列表 (return [fig]
)。无论哪种方式都应该可以正常工作。