4

I want to use m.add_layer for Popus from in for (as given here). However, it is not working as expected. My minimum working example is given below:

from shiny import App, render, ui
from shinywidgets import output_widget, reactive_read, register_widget
from ipywidgets import HTML
from ipyleaflet import Map, Marker, Popup

app_ui = ui.page_fluid(
    output_widget("m")
    )


def server(input, output, session):
    center = (52.204793, 360.121558)
    m = Map(center=center, zoom=9, close_popup_on_click=False)
    message1 = HTML()
    message1.value = "Try clicking the marker!"

# Popup with a given location on the map:
    popup = Popup(
    location=center,
    child=message1,
    close_button=False,
    auto_close=False,
    close_on_escape_key=False
    )
    
    m.add_layer(popup) # This line is not working
    register_widget("m", m)

app = App(app_ui, server)

Wondering what basic am I missing here?

M--
  • 25,431
  • 8
  • 61
  • 93
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • 3
    Same problem when using ` @output @render_widget` inside `server` as [in the shinywidgets github examples](https://github.com/rstudio/py-shinywidgets). `Map`and `Marker`work fine. There is [also an open issue about Popup](https://github.com/rstudio/py-shinywidgets/issues/75). – pholzm Feb 18 '23 at 20:01
  • 1
    Which issues are you having? – alexisdevarennes Feb 20 '23 at 13:31

1 Answers1

0

It looks that the m.add_layer(popup) line is not working because you are trying to use the ipyleaflet Map object as a Shiny widget, but Shiny does not recognize it. Instead, you can use the output_widget function from shinywidgets to create a Shiny widget from the ipyleaflet Map object, and then add the popup to the map using the add_layer method;

from shiny import App, render, ui
from shinywidgets import output_widget, reactive_read, register_widget
from ipywidgets import HTML
from ipyleaflet import Map, Marker, Popup

app_ui = ui.page_fluid(
    output_widget("m")
)


def server(input, output, session):
    center = (52.204793, 360.121558)
    m = Map(center=center, zoom=9, close_popup_on_click=False)
    message1 = HTML()
    message1.value = "Try clicking the marker!"

    # Popup with a given location on the map:
    popup = Popup(
        location=center,
        child=message1,
        close_button=False,
        auto_close=False,
        close_on_escape_key=False
    )

    m.add_layer(popup)

    output.m = output_widget("m", width="100%", height="500px")
    register_widget("m", m)

app = App(app_ui, server)
Bitz
  • 1