如何在 dash-leaflet 中使用 hover_feature 或 click_feature 将多个输入和输出分配给 app.callback?

How to assign multiple inputs and outputs to app.callback with hover_feature or click_feature in dash-leaflet?

我无法让多个输入和输出在 Dash-leaflet 中工作。本质上,我想执行以下操作:单击鼠标在地图上放置一个标记,当鼠标悬停在一个国家上时,用多边形突出显示地图的一个区域,并在不同的窗格中显示有关悬停在该国家/地区的一些信息。我在单独的 app.callback 中实现了这些功能中的每一个,如下面的代码所示。

app = dash.Dash(__name__)

app.layout = html.Div(children=[
    dl.Map(children=[dl.TileLayer(), 
                     
                     dl.LayerGroup(id="layer"),
                     
                     
                     dl.GeoJSON(url='https://pkgstore.datahub.io/core/geo-countries/countries/archive/23f420f929e0e09c39d916b8aaa166fb/countries.geojson', id="countries",
                                options=dict(style=dict(opacity=0,fillOpacity=0)),
                                hoverStyle=arrow_function(dict(weight=2, color='red', dashArray='', opacity=1)))
                     ],


            style={'width': '1000px', 'height': '500px'}, zoom=3, id='map'),
    
    html.Div(id='country'),
    
    html.Div(id='Country info pane')

])

@app.callback(Output("layer", "children"), [Input("countries", "click_feature")])
def map_click(feature):

    return [dl.Marker(position=[np.random.randint(-90,90),np.random.randint(-185,185)])]


@app.callback(Output('Country info pane', 'children'),
              Input('countries', 'hover_feature'))
def country_hover(feature):
    if feature is not None:
        country_id = feature['properties']['ISO_A3']

        return country_id

@app.callback(Output('layer', 'children'),
              Input('countries', 'hover_feature'))
def country_hover(feature):
    if feature is not None:
        return(dl.Polygon(positions=[[30,40],[50,60]], color='#ff002d', opacity=0.2))
      


if __name__ == '__main__':
    app.run_server(debug=False)

虽然这些回调中的每一个都独立工作(即如果我注释掉其他回调),但如果我 运行 代码中有两个或更多回调处于活动状态,则 none 它们可以工作。

如果我像下面这样组合两个 hover_feature 函数,它们似乎可以一起工作。

@app.callback(Output('Country info pane', 'children'),
              Output('layer', 'children'),
              Input('countries', 'hover_feature'))
def country_hover(feature):
    if feature is not None:
        country_id = feature['properties']['ISO_A3']

        return country_id, dl.Polygon(positions=[[30,40],[50,60]], color='#ff002d', opacity=0.2)

但是,“map_click”函数仍然不能与“country_hover”函数一起使用。如何让所有三个回调都起作用?

我找到了如何做到这一点,我不得不添加另一个 dl.LayerGroup 到具有不同 id 的 dl.Map 对象,然后分配每个函数输出到不同的层。