How do I use more ipywidgets than just the slider for a popup on ipyleaflet within a shiny python app?

92 Views Asked by At

In this demo app, I am trying to get create a popup for when I click on one of the polygons that I have drawn on the map with the geodataframe data. I have tried experimenting with many different ipywidgets to see how the different popups would look so that I can improve on the one that I decide to use. However, for some reason only the IntSlider widget seems to work. Whenever I substitute another widget for the slider widget, all of the polygons that are drawn disappear and there is no longer anything to click on to generate the popup. For now, I am okay with having the exact same popup for each polygon and will adjust that later. Are the other widgets not compatible? Or do I need to make some edits to my code to get the widgets to work?

from pathlib import Path
from shiny import App, ui, render, reactive
from shinywidgets import output_widget, register_widget, render_widget
import ipyleaflet as ipyl
from ipyleaflet import Map, Polygon, GeoData, basemaps, Popup
import ipywidgets as widgets
import geopandas as gpd


center_coords = [39.8282, -98.5795]

#data
raw_data = gpd.read_file(Path(__file__).parent / "data/reportingunits_regulatoryoverlays_v2/reportingunits_regulatoryoverlays_v2.shp")

# define ui
app_ui = ui.page_fluid(
    output_widget("my_map")
) #end page_fluid

# define server
def server(input, output, session):   

    my_map = ipyl.Map(basemap=basemaps.Esri.WorldTopoMap, center=center_coords, zoom=3)
    polygons = GeoData(geo_dataframe=raw_data, style={'weight': 0.5, 'fillOpacity': 0.1})
    register_widget("my_map", my_map)
    my_map.add_layer(polygons)

    label = Popup(child=widgets.Label("Hello World"))
    text = widgets.Text(value = 'Hello World', placeholder='Type something', description='String:', disabled=True )
    items = [widgets.Label(str(i)) for i in range(4)]
    box = widgets.Box(items)
    floatsinput = widgets.FloatsInput(value=[1.3, 4.56, 78.90], tag_style='info', format = '.2f')
    slider = widgets.IntSlider(value = 5, max = 10)
    string = "test"
    polygons.popup = label

# define app
app = App(app_ui, server)
0

There are 0 best solutions below