我的目标是获取用户点击绘图的坐标。 Plotly 确实支持 click 事件,但我无法让它在 pyscript 中工作。这是一个 MWE:
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<py-config>
packages = ['plotly','numpy','pandas']
</py-config>
</head>
<body>
<div id="chart1"></div>
<py-script>
import js, json, plotly
import plotly.express as px
fig = px.line(x=[1,2, 3], y=[1, 2, 3])
# fig.data[0].on_click(select_params) # <-- does not work
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
plot = js.Plotly.newPlot("chart1", js.JSON.parse(graphJSON))
def select_params(trace, points, selector):
print('Hello, World!')
</py-script>
</body>
</html>
有办法让它工作吗?