0

I'm using shiny and python for a project and I run into an interesting behavior.

My code should create a marker and equip this marker with a function once the marker is clicked.

from ipyleaflet import Marker, Map, MarkerCluster

map = Map(center=(0, 0), zoom=5, scroll_wheel_zoom=True)

def a(x):
    print(x)

x=0
markers = []

for i in range(5):
    marker = Marker(location = (0,0), draggable=False)
    marker.on_click(a(x))
    x+=1
    markers.append(mark)

marker_cluster = MarkerCluster(markers=markers)
map.add_layer(marker_cluster)

There is probably some issue within the for loop as when the map is plotted (I'm building a shiny app), it prints to terminal 0 and 1, but when I click on the marker nothing happens.

Thanks for your time

Beppe
  • 1
  • 1
  • In the `on_click()` call here, you should be passing a function, not calling it. You're calling `on_click()` with `None` (since your function has no return statement). See https://stackoverflow.com/q/66360839/3216427 for a fix. – joanis Aug 10 '23 at 21:11

0 Answers0