MYaseen208
MYaseen208

Reputation: 23938

shiny for Python using add_layer for Popus from ipyleaflet

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?

Upvotes: 5

Views: 774

Answers (2)

FilipW
FilipW

Reputation: 1535

This is because of an issue in ipywidgets. If you install a version of ipywidgets < 8 it will work. For example: pip install ipywidgets==7.6.5 made it work for me.

Upvotes: 1

Bitz
Bitz

Reputation: 1

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)

Upvotes: -2

Related Questions