Black Lives Matter. Please consider donating to Black Girls Code today.

No callback event triggered after exporting as a standalone html page

Hi guys,

I have tried a simple point selection example with the code below:

import plotly.graph_objects as go

import numpy as np

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

f = go.FigureWidget([go.Scatter(x=x, y=y, mode='markers')])
scatter =[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

It works well in my Jupyterbook and I can interactively make the selected points highlighted.

However, after I export this FigureWidget as a HTML (by using f.write_html(‘test.html’, auto_open=True) ), the callback event doesn’t work.

Do you know why this difference,thanks.:slight_smile:

Hi @Fantasyfans2012 welcome to the forum! The callback is a Python callback, so you need a Python kernel running (the Jupyter kernel in the case of the FigureWidget) to capture events from Javascript and execute the Python code of your callback. An HTML page does not know anything about Python so you just have a normal plotly figure, without the callback.

If you need to have callbacks in HTML pages, outside of the notebook, you can take a look at Dash which allows you to build web applications (with callbacks). Of course you will also need to have a Dash sever running.

1 Like

It makes sense, thanks a lot

1 Like