param.List with item_type=param.XYCoordinate

I would like to have a list of points (x,y) as a parameter on my class so that I can construct a numpy array from it of the proper share whenever it is modified. I have two questions

  1. This doesn’t appear to be the correct approach
class T(param.Parameterized):
    vertices = param.List(item_type=param.XYCoordinates)

    @param.depends("vertices", watch=True)
    def _update_points(self):
        self.coords = np.array(self.vertices).T

and allows me to append (2,2,2) to the list

  1. The @param.depends decorator never gets called when I append or extend the list but does when I replace it all together. Is there a way to trigger on adding a subitem to the list?

for list you will need to trigger manually the change of the parameter because appending an object to a list does not modify the list reference

t = T()
t.vertices.append((1,1))
t.param.trigger("vertices")
print(t.coords)
t.vertices.append((1,2))
t.param.trigger("vertices")
print(t.coords)

That seems to be the case for issue 2 after digging into it more.

Any thoughts on issue 1 where

item_type=param.XYCoordinates 

seems to be ignored and allows (2,2,2)? I still can’t seem to find a reason for that.

same problem, param does not see the list modified when appending so th validation routine is not triggered.
However when we trigger the change the error is raised:

and appending (2,2) will throw the same error since it’s a tuple and not a param.XYCoordinate

1 Like

I don’t think you can use Parameters (from Param) to set item_type in your code. Instead you could just use tuple or create your own Point class outside of T and use it to set item_type.

1 Like