如何处理 Databricks 中 Plotly 图表中的单击事件?

Eva*_*ith 6 plotly databricks plotly-python

我有兴趣创建一个托管在 Databricks 笔记本中的交互式图表。我一直在尝试 Plotly,但我不知道如何让任何自定义交互功能发挥作用。具体来说,我需要一个点击事件处理程序。我使用的以下代码基于Plotly 文档中的示例。唯一的区别是最后一行,我用它displayHTML()来渲染图形。

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))
Run Code Online (Sandbox Code Playgroud)

如何让自定义处理程序在调用.to_html()图形时持续存在?