posit-dev / py-shinywidgets

Render ipywidgets inside a PyShiny app
MIT License
47 stars 5 forks source link

Support for Folium widgets #71

Closed justinm0rgan closed 9 months ago

justinm0rgan commented 1 year ago

Description

Was trying to create a shiny app with interactive choropleth map, however Folium objects not accepted.

What I Did

Had to conduct extensive wrangling to get close to what I would want with ipyleaflet, would have been able to deploy the Folium example a day earlier and the result is actually better with Folium.

cpsievert commented 1 year ago

If you install the latest version of {htmltools} (pip install https://github.com/rstudio/py-htmltools/tarball/main), you'll be able to render folium maps directly using shiny's render.ui, for example:

import folium
from shiny import App, render, ui

from shinywidgets import *

app_ui = ui.page_fixed(
    ui.input_slider("zoom", "Zoom", 0, 20, 6, step=1),
    ui.output_ui("map")
)

def server(input, output, session):
    @output
    @render.ui
    def map():
        return folium.Map(location=(45.5236, -122.6750), zoom_start=input.zoom())

app = App(app_ui, server)

That said, since {folium} doesn't follow the ipywidgets protocol, you won't be able to achieve performant updates or react to client-side changes in the same way you can with a ipywidgets-compatible package like {ipyleaflet}.

cpsievert commented 9 months ago

Closing this since {shinywidgets} has nothing to add here (please file an issue in https://github.com/posit-dev/py-shiny if you encounter issues using @render.ui with {folium})