Ipyleaflet problem with Panel

I am trying to make an application where I need the on_interaction event handler for the ipyleaflet. It works fine with Panel + JupyterLab. However, when I run the same application as a standalone servable, it doesn’t work. It doesn’t seem to propagate the layer addition. In addition, if I pan the map right after interaction event, it generates error;
The code snippet is and the error is below:

import panel as pn

from ipyleaflet import Map, Rectangle, LayersControl, LayerGroup

pn.extension()

pn.extension('ipywidgets')

def handle_interaction(**kwargs):

    if kwargs.get('type') == 'click':

        rectangle = Rectangle(name='rect', bounds=((36, 26), (42, 45)))

        layer_group = LayerGroup(layers=(rectangle,))

        m.add(layer_group)

m = Map(center=(39.5, 31), zoom_snap=0.25, zoom_delta=0.25, zoom=5.5)

m.on_interaction(handle_interaction)

control = LayersControl(position='topright')

m.add_control(control)

ACCENT_COLOR = pn.template.FastListTemplate.accent_base_color

template = pn.template.FastListTemplate(

    site="Map", title="Map",

    sidebar=[],

    main=[m]

).servable();

The error is:

2022-08-29 16:45:08,837 Exception in widget method <function Widget._handle_msg at 0x7fbc888ed7a0>: 'NoneType' object has no attribute 'model_id'
Traceback (most recent call last):
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/ipywidgets/widgets/widget.py", line 237, in m
    return(method(self, *args, **kwargs))
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/ipywidgets/widgets/widget.py", line 756, in _handle_msg
    self.set_state(state)
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/ipywidgets/widgets/widget.py", line 625, in set_state
    self.set_trait(name, from_json(sync_data[name], self))
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/contextlib.py", line 119, in __exit__
    next(self.gen)
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/traitlets/traitlets.py", line 1348, in hold_trait_notifications
    value = trait._cross_validate(self, getattr(self, name))
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/traitlets/traitlets.py", line 729, in _cross_validate
    value = obj._trait_validators[self.name](obj, proposal)
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/traitlets/traitlets.py", line 1132, in __call__
    return self.func(*args, **kwargs)
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/ipyleaflet/leaflet.py", line 2377, in _validate_layers
    self._layer_ids = [layer.model_id for layer in proposal.value]
  File "/home/user/anaconda3/envs/panel_test/lib/python3.7/site-packages/ipyleaflet/leaflet.py", line 2377, in <listcomp>
    self._layer_ids = [layer.model_id for layer in proposal.value]
AttributeError: 'NoneType' object has no attribute 'model_id'

I suspect the error might be due to ipywidgets (error points to _validate_layer). However, the same example works in JupyterLab without any issues. Thats why I think it might be related to the Panel.

Thanks in advance.

1 Like