如何捕获Plotly的“Error displaying widget: model not found”错误?
捕获Jupyter中Plotly Widget的"model not found"显示错误
我需要测试Jupyter Notebook环境,有时Plotly图表无法正常显示,只会弹出错误提示:Error displaying widget: model not found。这个错误无法通过try-except代码块捕获,也无法在Out或_变量中查看具体信息,推测问题出在IPython.display.display方法内部。
我的需求是捕获该错误并据此修改当前单元格(或后续单元格)的输出,注意:我是要编写测试逻辑,而非修复这个显示错误本身。
以下是会触发该错误的示例代码:
import numpy as np import plotly.graph_objs as go from plotly.offline import iplot, init_notebook_mode from IPython.display import display class ClickableScatterPlotly: def __init__(self, data): self.scatter_fig = go.FigureWidget( [ go.Scatter(x=data[:,0], y=data[:,1], mode='markers', showlegend=False), ] ) self.scatter = self.scatter_fig.data[0] self.scatter.on_click(self.clicked) self.select_point(0) self.ui = self.scatter_fig def clicked(self, trace, points, selector): self.select_point(points.point_inds[0]) def select_point(self, i): n = len(self.scatter.x) c = ['blue'] * n c[i] = 'red' with self.scatter_fig.batch_update(): self.scatter.marker.color = c data = np.random.rand(50,2) click_scatter_plotly = ClickableScatterPlotly(data) display(click_scatter_plotly.ui)
内容的提问来源于stack exchange,提问作者ambrustorok
相关产品推荐
相关产品推荐

