使用plotly.graph_objects的FigureWidget()报错求助
解决Plotly FigureWidget在Dash/Flask环境中的报错问题
核心原因
go.FigureWidget()是专为Jupyter交互式环境开发的组件,依赖ipywidgets的交互框架,但Dash/Flask属于Web应用框架,两者运行环境不兼容,这是触发NotImplementedError的根本原因。
解决方案
方案1:替换为Dash兼容的图表组件
如果你的目标是在Dash应用中展示Plotly图表,直接用Dash原生的dcc.Graph组件替代FigureWidget,示例代码:
import dash from dash import dcc, html import plotly.graph_objects as go # 创建普通Plotly Figure(而非FigureWidget) countfig = go.Figure( data=[go.Bar(x=[1,2,3], y=[4,5,6])], layout=go.Layout(title="示例图表") ) app = dash.Dash(__name__) app.layout = html.Div([ dcc.Graph(figure=countfig) ]) if __name__ == '__main__': app.run_server(debug=True)
方案2:适配Jupyter环境(仅适用于交互式场景)
如果代码确实需要在Jupyter Notebook/Lab中运行,完成以下额外配置:
- 安装并启用Jupyter扩展:
pip install ipympl jupyter nbextension enable --py widgetsnbextension jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget - 重启Jupyter服务后再执行代码。
方案3:排查依赖版本冲突
若坚持要在非Jupyter环境使用FigureWidget,尝试调整依赖版本:
- 降级ipywidgets到7.x版本(8.x可能存在兼容性问题):
pip install ipywidgets==7.7.2 - 确保Plotly版本与ipywidgets匹配,建议安装Plotly 5.x系列:
pip install plotly==5.15.0
内容的提问来源于stack exchange,提问作者kot
相关产品推荐
相关产品推荐

