You need to enable JavaScript to run this app. 使用Plotly Dash绘制饼图时触发KeyError: 'outputs_list'报错如何解决
优惠活动
大模型
产品
解决方案
定价
更多

使用Plotly Dash绘制饼图时触发KeyError: 'outputs_list'报错如何解决

错误原因

你遇到的KeyError: 'outputs_list'和后续饼图无法显示的问题,由以下几个错误导致:

  • 错误调用被回调装饰器修饰的函数:被@app.callback装饰的pie_chart是Dash框架专属调用的函数,不支持开发者手动直接调用,你在main函数中写的pie_chart()是触发该报错的直接原因。
  • 饼图代码变量名错误:你在函数中先读取数据到df变量,后续传参时误用了不存在的dff变量,会触发变量未定义错误。
  • 饼图调用方法和参数错误:go.pie应为大写开头的go.Pie,且go类绘图不支持data_frame参数,同时values需要传入数值型字段,你填写的status是字符串类型的分类字段,无法作为饼图的数值维度。
  • 空输入未做拦截:文本输入框为空时,update_app_ui函数直接调用check_review会触发预测报错。
修复步骤
  1. 直接删除main函数中的pie_chart()代码行,Dash会在页面加载时自动触发该回调生成饼图,无需手动调用。
  2. 替换pie_chart函数的代码为正确写法:
# 需提前导入plotly.express:import plotly.express as px
@app.callback(
    Output('graph', 'figure')
)
def pie_chart():
    df = pd.read_csv('prediction.csv')
    # 统计正负情感的数量
    count_df = df['status'].value_counts().reset_index()
    count_df.columns = ['status', 'count']
    # 生成饼图
    piechart = px.pie(
        data_frame=count_df,
        values='count',
        names='status',
        title='情感预测结果分布'
    )
    return piechart
  1. 给update_app_ui函数增加空值拦截逻辑,避免空输入报错:
@app.callback(
    Output('result',  'children' ),
    [Input('textarea_review', 'value' )]
)
def update_app_ui(textarea_value):
    if not textarea_value:
        return ""
    print('Data Type of ', str(type(textarea_value)))
    print('Value = ', str(textarea_value) )
    response = check_review(textarea_value)
    print('response = ', response)
    if (response[0] == 0):
        result1 = 'Negative'
    elif  (response[0] == 1):
        result1 = 'Positive'
    else:
        result1 = 'Unknown'
    return result1
  1. 检查项目开头是否导入了所有依赖库:dash、dash_core_components as dcc、dash_html_components as html、dash_bootstrap_components as dbc、pandas as pd、pickle、sklearn对应的特征处理类、webbrowser等。

内容的提问来源于stack exchange,提问作者50. Dewang Patil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 09:24:06