I have jupyter notebook environments I want to test. Sometimes a plotly plot is not displayed, just an error message instead: "Error displaying widget: model not found". My problem is that this is no a valid error, you can not catch it with a try-except block, and you can not see the error in the Out
or the _
variables, because those just display the description of the model. So the issue probably lies within IPython.display.display
.
What I want to do, is to catch this error, and modify the output of the cell (or the cell after) based on this. So not trying to fix the error, because I am trying to write tests.
Here you can see a sample code that results in Error displaying widget: model not found
.
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)