import plotly.graph_objects as go
f = go.FigureWidget()
f.show()
This shows the figureWidget object as a static figure and not a dynamic widget-type plot, and so this doesn’t work as intended. My particular goal is to click on a point on a plotly mesh3D plot, which will then trigger some activity in other ipywidgets. For example, the following code will simply show the Cartesian coordinates of a clicked plotly point in some ipywidget text boxes:
import plotly.graph_objects as go
from plotly.callbacks import Points, InputDeviceState
from ipywidgets import widgets
points, state = Points(), InputDeviceState()
#Function to update widgets on plotly click
def pointPrint(trace, points, state):
global coordinates, xDisp, yDisp, zDisp
ind=points.point_inds
xDisp.value=coordinates[ind,0]
yDisp.value=coordinates[ind,1]
zDisp.value=coordinates[ind,2]
#define the trace
trace=go.Mesh3d(x=coordinates[:,0],y=coordinates[:,1],z=coordinates[:,2],
i=triangulation[:,0],j=triangulation[:,1],k=triangulation[:,2])
#Setup to ipywidgets
xDisp=widgets.FloatText(
value=0,
description='x',
disabled=True)
yDisp=widgets.FloatText(
value=0,
description='y',
disabled=True)
zDisp=widgets.FloatText(
value=0,
description='z',
disabled=True)
#show everything
fig=go.Figure(data=[trace])
f2=go.FigureWidget(fig)
scatter=f2.data[0]
scatter.on_click(pointPrint)
widgets.VBox([xDisp,yDisp,zDisp,f2])
This works seamlessly in a Jupyter notebook. In colab, simply nothing happens… there are no errors or messages, simply nothing is displayed. If the ‘f2’ in the last line of code above is removed and the code is run again, then the ipywidgets show as intended with the specified initial value of zero.