How to export a plotly figure with on_click events to html?

I’m wondering if there’s a way to export FigureWidget objects to html while retaining the on_click functionality. My workflow: I create a FigureWidget with an on_click event using the code from this tutorial, then I export the FigureWidget to html using plot() from plotly.offline. My issue is that the exported html won’t have the on_click functionality. Is there a way to do this? Thank you.

My complete code:

import plotly.graph_objects as go
from plotly.offline import plot
import numpy as np

np.random.seed(1)

x = np.random.rand(100)
y = np.random.rand(100)

f = go.FigureWidget([go.Scatter(x=x, y=y, mode='markers')])

scatter = f.data[0]
colors = ['#a3a7e4'] * 100
scatter.marker.color = colors
scatter.marker.size = [10] * 100
f.layout.hovermode = 'closest'


# create our callback function
def update_point(trace, points, selector):
    c = list(scatter.marker.color)
    s = list(scatter.marker.size)
    for i in points.point_inds:
        c[i] = '#bae2be'
        s[i] = 20
        with f.batch_update():
            scatter.marker.color = c
            scatter.marker.size = s


scatter.on_click(update_point)

# Export to html and display in browser. Expecting "on click" functionality to work
plot(f)
1 Like