Plotly Dash开发仪表盘无报错但图表不显示问题咨询
问题原因
- 核心问题:回调函数未对输入参数做非空校验
你开启了app.config.suppress_callback_exceptions = True配置,当用户未同时选中报表类型和年份时,参数year为None,代码执行int(year)会触发类型错误,异常会被静默吞掉,没有返回任何图表组件,所以页面没有报错但保持空白。 - 次要问题:你已经导入了
no_update但完全未使用,没有处理参数未选择完成的场景。
修复方案
在回调函数get_graph的最开头添加非空校验即可,原有逻辑不需要修改:
def get_graph(chart, year, children1, children2, c3, c4, c5): # 新增非空判断:两个参数都选择完成才继续执行 if chart is None or year is None: return no_update, no_update, no_update, no_update, no_update # 原有代码完全保留 df = airline_data[airline_data['Year']==int(year)] # ... 后续逻辑不用动
可选优化(解决导入警告)
如果你的Dash版本是2.0以上,可以把旧版独立导入替换为统一导入,避免弃用警告:
# 替换原有 import dash_html_components as html、import dash_core_components as dcc from dash import html, dcc
如果本地测试端口冲突,可以修改运行配置:
if __name__ == '__main__': app.run_server(debug=False, port=8050)
内容的提问来源于stack exchange,提问作者Gavin Wong
相关产品推荐
相关产品推荐

