5

I am interested in creating an interactive chart that is hosted in a Databricks notebook. I have been experimenting with Plotly, but I can't figure out how to get any custom interactivity working. Specifically, I need to have a click event handler. The following code I am using is based on this example from Plotly's documentation. The only difference is the last line, where I use displayHTML() to render the figure.

import plotly.graph_objects as go

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)

displayHTML(f.to_html(include_plotlyjs='cdn', full_html=False))

How can I get custom handlers to persist when calling .to_html() on a figure?

Evan Smith
  • 97
  • 7

0 Answers0