You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dash多标签页基于印度邦下拉选择动态生成图表的回调实现问询

问题原因

你当前代码运行失败的核心原因是:回调声明了6个图表输出项,但每个条件分支仅返回对应标签页的2个图表对象,返回值数量和声明的输出数量不匹配,触发回调参数校验错误。

你不需要引入dcc.State解决该问题,直接返回所有6个生成好的图表即可:非当前激活标签页的图表即使更新了数据也不会在页面渲染,不会造成额外性能损耗,也不影响使用体验。

修正后的回调函数

@app.callback([Output('graph1_convicts', 'figure'),
               Output('graph2_convicts', 'figure'),
               Output("graph1_under_trial", "figure"),
               Output("graph2_under_trial", "figure"),
               Output("graph1_detenues", "figure"),
               Output("graph2_detenues", "figure")],
              [Input('dropdown', 'value')])

def update_graph(selected_state):
    # 已决犯数据处理与绘图
    df1_convicts = df[df["state_name"]==selected_state]
    df1_convicts = df1_convicts.drop(["is_state", "caste", "under_trial", "detenues", "others"], axis=1)
    df1_convicts = df1_convicts.groupby(['state_name', "year", "gender"])['convicts'].sum().reset_index()

    df2_convicts = df[df["state_name"]==selected_state]
    df2_convicts = df2_convicts.drop(["is_state", "gender", "under_trial", "detenues", "others"], axis=1)
    df2_convicts = df2_convicts.groupby(["state_name", "year", "caste"])["convicts"].sum().reset_index()

    fig1_convicts = px.bar(df1_convicts, x="year", y="convicts", color="gender", title="已决犯性别分布",
                            color_discrete_sequence=px.colors.qualitative.Set1, opacity=0.6)
    fig2_convicts = px.bar(df2_convicts, x="year", y="convicts", color="caste", title="已决犯种姓分布",
                            color_discrete_sequence=px.colors.qualitative.Set1, opacity=0.6)

    # 未决犯数据处理与绘图
    df1_under_trial = df[df["state_name"]==selected_state]
    df1_under_trial = df1_under_trial.drop(["is_state", "caste", "convicts", "detenues", "others"], axis=1)
    df1_under_trial = df1_under_trial.groupby(['state_name', "year", "gender"])['under_trial'].sum().reset_index()

    df2_under_trial = df[df["state_name"]==selected_state]
    df2_under_trial = df2_under_trial.drop(["is_state", "gender", "convicts", "detenues", "others"], axis=1)
    df2_under_trial = df2_under_trial.groupby(["state_name", "year", "caste"])["under_trial"].sum().reset_index()

    fig1_under_trial = px.bar(df1_under_trial, x="year", y="under_trial", color="gender", title="未决犯性别分布",
                                color_discrete_sequence=px.colors.qualitative.Set1, opacity=0.6)
    fig2_under_trial = px.bar(df2_under_trial, x="year", y="under_trial", color="caste", title="未决犯种姓分布",
                                color_discrete_sequence=px.colors.qualitative.Set1, opacity=0.6)

    # 被拘留者数据处理与绘图
    df1_detenues = df[df["state_name"]==selected_state]
    df1_detenues = df1_detenues.drop(["is_state", "caste", "convicts", "under_trial", "others"], axis=1)
    df1_detenues = df1_detenues.groupby(['state_name', "year", "gender"])['detenues'].sum().reset_index()

    df2_detenues = df[df["state_name"]==selected_state]
    df2_detenues = df2_detenues.drop(["is_state", "gender", "convicts", "under_trial", "others"], axis=1)
    df2_detenues = df2_detenues.groupby(["state_name", "year", "caste"])["detenues"].sum().reset_index()

    fig1_detenues = px.bar(df1_detenues, x="year", y="detenues", color="gender", title="被拘留者性别分布",
                            color_discrete_sequence=px.colors.qualitative.Set1, opacity=0.6)
    fig2_detenues = px.bar(df2_detenues, x="year", y="detenues", color="caste", title="被拘留者种姓分布",
                            color_discrete_sequence=px.colors.qualitative.Set1, opacity=0.6)

    # 按Output声明的顺序返回所有6个图表
    return fig1_convicts, fig2_convicts, fig1_under_trial, fig2_under_trial, fig1_detenues, fig2_detenues

原来的回调把标签页选中值作为输入是多余的,因为我们不管当前在哪个标签页都更新所有图表,所以可以直接把Input("tabs-selector", "value")从输入项里删掉。

可选优化建议

你可以把重复的数据处理和绘图逻辑封装成通用函数,减少冗余代码,后续修改逻辑也只需要改一处即可。


内容的提问来源于stack exchange,提问作者klaptor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 15:30:01