如何在ipyleaflet地图中更新带有标题的pyshing文本输入



每次在ipyleaflet地图小部件中使用标记的标题值单击标记时,我都想更新pyshing应用程序中的文本输入。我一直在使用marker.on_click((回调来从单击的标记中获取标题(在本例中为station_id(,但我很难用标题文本动态更改文本输入框。

下面是我尝试过的一个基本的工作示例。每次单击callback((函数时,都会将每个标记的标题打印到屏幕上。但是,我不知道如何更新ui.input_text的值。Reactive.Effect函数似乎根本没有被调用,我不知道为什么。我需要在这里更改什么来更新文本输入中的值?

from shiny import App, reactive, run_app, ui
from shinywidgets import output_widget, register_widget
import ipyleaflet as ipyl

app_ui = ui.page_fluid(
{"class": "p-4"},
ui.row(
ui.column(
4,
ui.input_text("station_id", "Station ID:", value="Click on a station marker"),
),
ui.column(
8,
output_widget("map_widget")
),
),
)

def get_station_id(_marker):
def callback(*args, **kwargs):
print(_marker.title)
return _marker.title
return callback

def add_stations_to_map(_map, stations):
markers = []
for station in stations:
marker = ipyl.Marker(location=station[0], draggable=False, title=station[1])
_map.add(marker)
markers.append(marker)
return markers

def server(input, output, session):
# Initialize and display when the session starts (1)
_map = ipyl.Map(center=(-33.8, 151), zoom=8, scroll_wheel_zoom=True)
# Add a distance scale
_map.add(ipyl.leaflet.ScaleControl(position="bottomleft"))
register_widget("map_widget", _map)
# add markers and station id clik function
stations = [
[(-33.8, 151), "station_1"],
[(-33.8, 150), "station_2"],
]
markers = add_stations_to_map(_map, stations)
clicked_marker = "Click on a station marker"
for marker in markers:
clicked_marker = marker.on_click(get_station_id(marker))
@reactive.Effect
def _():
ui.update_text("station_id", value=clicked_marker)

app = App(app_ui, server, debug=True)
run_app(app, launch_browser=True, port=0)

您可以使用反应式。设置文本的值:

clicked_marker_value = reactive.Value("Click on a station marker")
def get_station_id(_marker):
def callback(*args, **kwargs):
print(_marker.title)
clicked_marker_value.set(_marker.title)
return _marker.title
return callback
@reactive.Effect
def _():
ui.update_text("station_id", value=clicked_marker_value())

最新更新