Search code examples
pythongisgeopandasfolium

How do add drop down menu for the folium map?


For example, given the various months columns containing temperatures per specific region, how can I change the heatmap based on each month using drop down menu? I already have a map built using the geodataframe.explore(column = 'col_name').

Thanks!


Solution

  • If you're working in jupyterlab, this is pretty easy! You can combine a ipywidgets.Output widget with a ipywidgets.Dropdown to set up your canvas, then capture the follium map with the output. You then need to link them by adding an event handler.

    import geopandas as gpd
    import ipywidgets
    from IPython.display import HTML, display
    
    df = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    
    out = ipywidgets.Output(layout={'border': '1px solid black'})
    
    w = ipywidgets.Dropdown(
        options=df.columns.values.tolist(),
        value=df.columns.values[0],
        description='Column:',
        disabled=False,
    )
    
    def on_dropdown_change(change):
        out.clear_output()
        with out:
            display(df.explore(w.value, cmap="Blues"))
    
    w.observe(on_dropdown_change, names='value')
    display(w)
    
    with out:
        display(df.explore(df.columns[0], cmap="Blues"))
    
    out
    

    folium/leaflet world map with dropdown widget for selecting columns