Black Lives Matter. Please consider donating to Black Girls Code today.

Invalid argument error`figure.layout` passed into Graph with ID "graph". Expected `object`. Was supplied type `array`

I am not sure if this is a bug or something, but when I host the app using app.run_server(debug=True), I will get the following error:

Invalid argument figure.layout passed into Graph with ID “graph”.
Expected object.
Was supplied type array.

However if I used Twisted (a simple version of Twisted Web server), the error will go away and works accordingly.

app = dash.Dash(__name__, server = server, 
                external_stylesheets=[dbc.themes.BOOTSTRAP])
server = flask.Flask(__name__)
twisted = Twisted(server)
twisted.run(host='0.0.0.0',port=8082, debug=True, run_reactor=True)

My layout and callback section:

app.layout = html.Div([
    html.Div(
    	className='row',
    	children=[
    		html.Div(
			className='col',
			children=[
			dcc.Dropdown(
            id="variable",
            options = hensu2,
            placeholder = 'something',
            multi=False)  
 			],style={'width': '35%','display': 'inline-block', 'height': '50px'}, 
    		),
    		html.Div(
    			className='col',
    			style={'width': '10%', 'display': 'inline-block', 'height': '50px'},
    			children=[
                    html.Img(src='data:image/png;base64,{}'.format(encoded_image.decode()),
             height = 30)],
    		),
    	],
        style={'width': '100%'}
    ),    
    html.Div(    
    dcc.Dropdown(
        id="machine",
        options = machines_list,
        placeholder = 'No',
        multi=True
    ),style={'width': '49%', 'display': 'inline-block'}),
    html.Div(
    dcc.Dropdown(id="size",
        options = size_list,
        placeholder = 'size',
        multi=True
    ), style={'width': '51%', 'display': 'inline-block'}),
    html.Div([
    dcc.Graph(
        id='graph'
    )])
]
)


@app.callback(
    Output(component_id='graph', component_property='figure'),
    [Input(component_id='variable', component_property='value')]
)
def update_output_div(input_value):
    if not input_value:
        return {'data':[], 'layout':[]}
    df0 = df.copy()
    X = df0["Date"]
    Y = df0[switch(input_value)]
    fig = {
    'data': [py.Scattergl(
        x=X,
        y=Y,
        mode='markers',
        opacity=0.7,
        marker={'size': 10}
    )],
    'layout': py.Layout(
        title={'text': input_value},
        xaxis={'title': 'Date'},
        yaxis={'title': input_value},
        hovermode='closest'
        )}
    return fig

I am a little bit concerned about this error. Is this something critical or something can be safely ignored? Previous similar post had the answer of an extra comma, I checked the code several times and did not find the extra comma, is this a different problem or did I missed it?

What’s py? That’s what the error message is referring to. That’s returning an array (list) instead of an object (dict)

1 Like

Sorry I just do not like import the alias as “go”. So I always do

import plotly.graph_objects as py

Edit to prevent double post: Your are absolutely correct. The Layout I am returning is missing a pair of square brackets and that is why it was returning error. Quite amazing that Twisted can run it without error. I was confused what is meant by “object” since it is the most generic type.