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

使用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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 18:42:03