Hello Community,
I’m trying to create a plot where users can:
- Show a ploygon selection/ scatter originally.
- Draw a polygon on an image.
- Drag the vertices of the polygon.
- Print the updated coordinates of the vertices dynamically
I was able to achieve it on dash trough call backs. However, I now want to have a plot per code cell, so dash app is not an option anymore. I am having trouble to get the call back functions to work. Can someone help me out?
For context, my plotly version is 6.1.0, environment is VS Code Jupyter Notebook.
A demo code of the ideal outcome:
import plotly.express as px
from skimage import data
img = data.chelsea()
fig = px.imshow(img)
fig.add_selection(path="M2,6.5L250,300L200,200L200,100Z", line=dict(color="black"))
fig.update_layout(dragmode="select", newselection=dict(line=dict(color="blue")))
fig.show()
In this fig, the vertices are draggable. However, I cannot find out how to print the updated vertices.
plotly 6.0.1 documentation: on_click shows that traces like scatter has this on_click method.
To check this function, I derived this code from the sample code in the documentation.
import plotly.graph_objects as go
from plotly.callbacks import Points, InputDeviceState
# Jupyter-only
fig = go.FigureWidget(
data=[go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="markers")],
layout=dict(title="Click a point"),
)
trace = fig.data[0]
def handle_click(trace, points, state:InputDeviceState):
print("handle_click triggered.")
trace.on_click(handle_click)
fig
However, I have an error saying this method is “not accessible with the class FigureWidget”.
Is scatter trace is not a baseTraceType?
Is there any alternative way to achieve the goal “print the coordinates of the vertices when they are dragged”?