eodaGmbH / py-maplibregl

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

MapWidget to_html() method not working #40

Closed giswqs closed 2 months ago

giswqs commented 2 months ago

It generates the HTML file, but the data layers do not show up.

import webbrowser

from maplibre.ipywidget import MapWidget as Map
from maplibre import Layer, LayerType, MapOptions
from maplibre.sources import GeoJSONSource

vancouver_blocks = GeoJSONSource(
    data="https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/geojson/vancouver-blocks.json",
)

map_options = MapOptions(
    center=(-123.1256, 49.24658),
    zoom=12,
    hash=True,
    pitch=35,
)

m = Map(map_options)
m.add_layer(
    Layer(
        type=LayerType.LINE,
        source=vancouver_blocks,
        paint={"line-color": "white"},
    )
)

temp_file = "/tmp/pymaplibregl.html"

with open(temp_file, "w") as f:
    f.write(m.to_html(style="height: 800px;"))

webbrowser.open(temp_file)

image

giswqs commented 2 months ago

I noticed that the add_control() method does not work either.

from maplibre.ipywidget import MapWidget as Map
from maplibre.controls import NavigationControl, FullscreenControl

m = Map()
m.add_control(NavigationControl())
m

image

crazycapivara commented 2 months ago

@giswqs In your example on top where you want to create your html output, you must import the Map instead of the MapWidget:

# Replace
# from maplibre.ipywidget import MapWidget as Map
# with
from maplibre import Map
giswqs commented 2 months ago

I know using from maplibre import Map works for creating HTML. One issue is that for users working with Jupyter notebook, how do they export the interactive map as an HTML file? The delimna is that you can see the interactive map, but you can't export it directly to HTML unless you go back to the notebook and change from maplibre.ipywidget import MapWidget as Map to from maplibre import Map

crazycapivara commented 2 months ago

Ah ok. You are right. This is not supported yet. I have to add this functionality.

crazycapivara commented 2 months ago

Furthermore, I cannot reproduce your error with missing controls. Navigation control is added by default and if I add it once again, I got 2 of them :-)

image

giswqs commented 2 months ago

I know using from maplibre import Map works for creating HTML. One issue is that for users working with Jupyter notebook, how do they export the interactive map as an HTML file? The delimna is that you can see the interactive map, but you can't export it directly to HTML unless you go back to the notebook and change from maplibre.ipywidget import MapWidget as Map to from maplibre import Map

That would be great! It will make it easier to integrate maplibre with other web apps like streamlit and solara.

crazycapivara commented 2 months ago

@giswqs Just forgot that it already works, you just need to activate the message queue:

from maplibre.ipywidget import MapWidget as Map

m = Map()
m.use_message_queue()
# ...
giswqs commented 2 months ago

Nice! It indeed works. The caveat is that m.use_message_queue() needs to be placed immediately after the map creatation. It won't work if placed after m.add_layer().