Unless I am misunderstanding how modifying shapes change the RelayoutData dictionary, I think I’m having an issue: Some of the examples on the annotations page are no longer working (example, drawing a ROI and displaying the histogram: Image Annotations | Dash for Python Documentation | Plotly). I’m confused by the dictionary values of RelayoutData after modifying a shape.
In the final example (Image Annotations | Dash for Python Documentation | Plotly), when the shape is first made, the values shapes[0].x0, shapes[0].x1, shapes[0].y0, shapes[0].y1 exist within the shapes dictionary.
"shapes: [{
'x0': 109.81480577256943,
'y0': 195.22337962962962,
'x1': 240.92591688368054,
'y1': 60.40856481481481
}]"
OK, this is how I expect the dictionary to be displayed and this is how most of the examples on the annotations page extract the coordinates. However, when I modify the shape, the relayoutData dictionary is no longer nested under shapes, but instead shows the modified shapes in the following format:
{
"shapes[0].x0": 22.887654320987643,
"shapes[0].x1": 349.05061728395054,
"shapes[0].y0": 114.11214814814814,
"shapes[0].y1": 426.368938271605
}
where there is now a string “shapes[0].x0” that has the updated coordinate value.
Any logic in a callback (such as the code shown below from the annotations example of plotting histogram from a ROI) will no longer work. Instead there would need to be a check for the string “shapes[0].x1” etc.
@callback(
Output("histogram", "figure"),
Input("graph-pic-camera", "relayoutData"),
prevent_initial_call=True,
)
def on_new_annotation(relayout_data):
if "shapes" in relayout_data:
last_shape = relayout_data["shapes"][-1]
# shape coordinates are floats, we need to convert to ints for slicing
x0, y0 = int(last_shape["x0"]), int(last_shape["y0"])
x1, y1 = int(last_shape["x1"]), int(last_shape["y1"])
roi_img = img[y0:y1, x0:x1]
return px.histogram(roi_img.ravel())
else:
return no_update
Is this a bug or is this the nature of the RelayoutData dictionary when modifying a shape? i.e. am I missing something here?
Many thanks for any help with understanding this, I hope my first post is not me mis-understanding something simple!
Richard