eodaGmbH / py-maplibregl

Python bindings for MapLibre GL JS
https://eodagmbh.github.io/py-maplibregl/
MIT License
27 stars 2 forks source link

Map widget missing layers when displayed more than once #82

Open giswqs opened 5 days ago

giswqs commented 5 days ago

When rendering the map object multiple times in a Jupyter notebook, layers only show up the first time when it is displayed.

import ipywidgets as widgets

from maplibre import Layer, LayerType
from maplibre.sources import GeoJSONSource
from maplibre.controls import ScaleControl, Marker
from maplibre.ipywidget import MapWidget as Map

# Create a source
earthquakes = GeoJSONSource(
    data="https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"
)

# Create a layer
layer_id = "earthquakes"

earthquake_circles = Layer(
    type=LayerType.CIRCLE,
    id=layer_id,
    source=earthquakes,
    paint={"circle-color": "yellow"}
)

# Render map
m = Map(height="200px")
m.add_control(ScaleControl(), position="bottom-left")
m.add_layer(earthquake_circles)
m.add_tooltip(layer_id, "mag")
m.add_marker(Marker(lng_lat=(100.507, 13.745)))
m

image

crazycapivara commented 5 days ago

Yep, that's the expected behavior. After the map is rendered, the message queue is cleared!

giswqs commented 5 days ago

Is it possible to retain the message queue?

One use case is that users may want to see the map first before saving it to an HTML file. However, once the map is rendered, saving the map to an HTML file does not work properly anymore because the layers are gone.

crazycapivara commented 5 days ago

Yes, this seems to be a special use case for the ipywidget. We can store it This also makes it possible to get information about layers etc from the python object even after the map has rendered. So I will keep this issue open.