How to edit / delete shapes created using a callback?

Hi @AIMPED thanks for your respones. If I understand what you’re saying correctly - when creating a shape, include a custom_name in the name attribute, that way I can create a list of names?

You can then delete a name from the list, and update the shapes based on the list (ie: using a for loop using name in list)?

Hopefully my understanding is correct. I believe I’m clear on what you’re saying, except the part of using the list to update the shapes. Could you please elaborate?

Here’s what I’ve learned so far, from the example you provided as well as this example here:

The following codes tells you how many shapes you have in your figure object:

# Find out how many shapes have been stored
len(fig.layout.shapes)

In my case it returned 5, the number of circles I have in the image.

I can get the properties of each shape as follows:

# Get the property of an added shape
# https://community.plotly.com/t/the-layout-shapes-property-does-not-update-after-drawing-shapes-with-the-mouse/61739
fig.layout.shapes[0]

OUTPUT:

layout.Shape({
‘editable’: True,
‘fillcolor’: ‘crimson’,
‘line’: {‘color’: ‘black’},
‘opacity’: 0.5,
‘type’: ‘circle’,
‘x0’: 296.0,
‘x1’: 306.0,
‘xref’: ‘x’,
‘y0’: 132.0,
‘y1’: 142.0,
‘yref’: ‘y’
})

# Properties of the last shape in the list
fig.layout.shapes[-1]

OUTPUT:

layout.Shape({
‘editable’: True,
‘fillcolor’: ‘crimson’,
‘line’: {‘color’: ‘black’},
‘opacity’: 0.5,
‘type’: ‘circle’,
‘x0’: 337.0,
‘x1’: 347.0,
‘xref’: ‘x’,
‘y0’: 199.0,
‘y1’: 209.0,
‘yref’: ‘y’
})

I can even do things like change the fill color of each circle using a for loop:

for index in range(len(fig.layout.shapes)):
    print(index)
    print(fig.layout.shapes[index])
    print()
    
    fig.layout.shapes[index].fillcolor = 'green'

However it’s still not clear to me how to delete a shape. I know I can make the list you mention, but I’m not sure how to implement it in updating the shapes. In the future the circle sizes are going to vary according to the size of the blemish, so I have to be careful when doing updates of properties to each shape.

I noticed there’s a pop method, but the doc string isn’t clear to me:

Signature: fig.layout.activeshape.pop(key, *args)
Docstring:
Remove the value associated with the specified key and return it

Parameters

key: str
Property name
dflt
The default value to return if key was not found in object

Returns

value
The removed value that was previously associated with key

Raises

KeyError
If key is not in object and no dflt argument specified
File: ~/.virtualenvs/computer_vision/lib/python3.10/site-packages/plotly/basedatatypes.py
Type: method

I’d really appreciate it if you could show me your method of deleting a shape, I think I got the rest covered now.

Cheers!