簡體   English   中英

使用 Dash Python 時出現錯誤 — 將無效參數“figure”傳遞到 ID 為“graph”的 Graph。 預期的“對象”。 提供類型“數組”

[英]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 {}, {} 你應該在這里只返回一個字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM