0

i'm trying to create a Shiny app that every time I click on a marker, it returns the id of the marker. My code is:

from shiny import App, reactive, run_app, ui
from shinywidgets import output_widget, render_widget
import ipyleaflet as L
import pandas as pd


app_ui = (
    ui.page_fluid(
        4,
        ui.output_text_verbatim("id"),
        output_widget("map"),
    ),
)


def server(input, output, session):
    def click(_id):
        global id
        id = _id

    @output
    @render_widget
    def map3():
        m = L.Map(center=(0, 0), zoom=5, scroll_wheel_zoom=True)
        m.add_control(L.leaflet.ScaleControl(position="topright"))

        d = {"lat": [0, 40], "lon": [0, 40], "id": [1, 2]}
        db = pd.DataFrame(data=d)

        for i in range(0, db.shape[0]):
            mark = L.Marker(
                location=(
                    db["lat"].iloc[i],
                    db["lon"].iloc[i],
                ),
                draggable=False,
            )
            id_ = db["id"].iloc[i]
            mark.on_click(click(id_))

            m.add_layer(mark)
        return m

    @output
    @render_widget
    def id():
        return f"id: {id}"


app = App(app_ui, server, debug=True)

my idea was to set a global variable id that is update everytime a marker is clicked, but it doesn't work.

Thanks everyone for his time

Beppe
  • 1
  • 1

0 Answers0