Error when trying to produce a Graph in Plotly Dash

When trying to produce a figure with the following code

@app.callback( [ Output("selected-plot", "figure") ], [ Input("submit-selected-plotting", "n_clicks"), State("table", "data") ],
)
def plot(button_clicked, data) fig = go.Scatter(x=data["index"], y=data["result"], mode='lines', name='result') return fig

and

 dbc.Col( [ dcc.Graph(id='selected-plot') ], width=6, )

I get a strange error with the app expecting a different object:

dash._grouping.SchemaTypeValidationError: Schema: [<Outputselected-plots.figure>] Path: () Expected type: (<class 'tuple'>, <class 'list'>) Received value of type <class 'plotly.graph_objs._scatter.Scatter'>: Scatter({...})

I have tried everything but I can't seem to go around this error. Thanks for any suggestions in advance!

1 Answer

The error is due to the fact that the app expects a figure object, you can fix it by updating the callback as follows:

@app.callback( [ Output("selected-plot", "figure") ], [ Input("submit-selected-plotting", "n_clicks"), State("table", "data") ],
)
def plot(button_clicked, data) trace = go.Scatter( x=data["index"], y=data["result"], mode='lines', name='result' ) return [go.Figure(data=trace)]
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like