5

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: [<Output selected-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 1

12

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)]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. Unfortunately that did not fix the problem. The traceback actually says that a graph object is received, but it expects something different apparently and I have no idea why.
I think it's because it expects a list. I have just updated my answer, let me know if it works now.
That worked! I have no idea why it expects a list, as it should not by default, anyway very thankful.
It's because Output("selected-plot", "figure") is enclosed in square brackets, if you remove them then my previous answer will work.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.