html 对于Python,使用来自ipylefleaf的add_layer用于Popus

6fe3ivhb  于 2023-02-27  发布在  Python
关注(0)|答案(1)|浏览(99)

我想使用m.add_layer for Popus from ipyleaflet in shiny for pythonas given here)。但是,它没有按预期工作。我的最小工作示例如下所示:

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)

想知道我错过了什么?

ikfrs5lh

ikfrs5lh1#

看起来m.add_layer(popup)行不起作用,因为您试图将ipyleaflet Map对象用作Shiny小部件,但Shiny无法识别它,因此您可以使用shinywidgets中的output_widget函数从ipyleaflet Map对象创建一个Shiny小部件,然后使用add_layer方法将弹出窗口添加到Map中;

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)

相关问题