使用 Dash Python 时出现错误 -- 无效参数“figure”传递到 ID "graph" 的 Graph 中。预期的“对象”。提供的类型为“array”

Getting an error with Dash Python -- Invalid argument `figure` passed into Graph with ID "graph". Expected `object`. Was supplied type `array`

我已经检查了与我面临的错误相关的其他类似问题。我似乎不明白为什么我一直收到此错误:

Invalid argument `figure` passed into Graph with ID "graph".
Expected `object`.
Was supplied type `array`.
Value provided: 
[
  {},
  {}
]

这是我要更新的图形的以下代码。本质上,有一个包含名称列表的下拉菜单,图表应更新为新图形,其中包含与所选下拉值相关的主题。


# main component of dashboard
BODY = dbc.Container(
    [

        dcc.Dropdown(
            id='xaxis-column',
            options=[{'label': i, 'value': i}
                     for i in sorted(user_list['displayName'].unique())],
        ),
        dbc.Card(dcc.Graph(id="graph")),
      
    ], className="mt-12")


@app.callback(
    Output('graph', 'figure'),
    [Input('xaxis-column', 'value')]
)
def update_graph(xaxis_column_name):

    print(xaxis_column_name)

    df = pd.json_normalize(dataframe['user'])
    df['parsedMessage'] = dataframe['parsedMessage']
    df['timestamp'] = dataframe['timestamp']
    df_one_user = df['displayName'] == xaxis_column_name

    dff = df[df_one_user]

    messages = list(
        dff["parsedMessage"].dropna().values)

    if len(messages) < 1:
        return {}, {}

    text = " ".join(list(messages))

    stop_words = ['will', 'reply', 'think', 'http',
                  'https', 'in reply', 'the us', 'us', 'now'] + list(STOPWORDS)

    wc = WordCloud(stopwords=stop_words, max_words=25, max_font_size=80)
    wc.generate(text)

    word_list, freq_list, fontsize_list, position_list, orientation_list, color_list = [
    ], [], [], [], [], []

    for (word, freq), fontsize, position, orientation, color in wc.layout_:
        word_list.append(word)
        freq_list.append(freq)

    word_list_top = word_list[:10]
    freq_list_top = freq_list[:10]

    word_df = pd.DataFrame(
        {'word_list': word_list_top,
         'freq_list': freq_list_top,
         })
    # print(word_df)

    fig = px.bar(word_df, x='word_list', y='freq_list')

    return fig

如果我不执行回调,只是硬编码 xaxis_column_name 的值,那么它就可以工作。

这是我的应用布局:

app.layout = html.Div(children=[NAVBAR, html.Br(className="my-2"), BODY])

我认为您的问题出在 return {}, {}。你应该 return 这里只有一个字典。