jupyter-widgets / ipyleaflet

A Jupyter - Leaflet.js bridge
https://ipyleaflet.readthedocs.io
MIT License
1.48k stars 363 forks source link

Possiblity to load image in popup on click #1092

Open nikste opened 1 year ago

nikste commented 1 year ago

Is this possible? I have a large collection of images taken at different positions. For each position i have a marker and want to show the image on markerclick without loading them upfront.

I've tried to use callbacks or override different methods, but so far been unsuccessful. Its a bit opaque to me where i could hook in some file loading logic and change the popup on the fly. Please help! (also thanks)

nikste commented 1 year ago

So I have found a workaround, but I'm not sure if that's the intended way. Essentially creating a popup in the callback on the fly manually:

m=Map(..)

def callback_with_popup_creation(fname):
  def f(**kwargs):
    marker_center = kwargs['coordinates'] 
    html = HTML()
    html.value="<img class=\"awesome image\" src=\"" + str(img_file_relative) + "\">"
    popup = Popup(
      location=marker_center,
      child=html
    )
    m.add_layer(popup)
  return f

for fname, lat, lon in data:
  marker = Marker(location = (lat, lon))   
  marker.on_click(callback(fname))

May this hopefully help the next poor person struggling like me.