posit-dev / py-htmltools

Tools for HTML generation and output
MIT License
19 stars 2 forks source link

Allow objects with a `_repr_html_()` method to be rendered to HTML #74

Closed cpsievert closed 10 months ago

cpsievert commented 10 months ago

As we've pointed out in several py-shiny/py-shinywidget issues (e.g., https://github.com/posit-dev/py-shiny/issues/303), rendering "ad hoc (i.e., non-ipywidget) widgets" like folium or itables is possible, but it isn't easy since to do because you need ui.HTML(x._repr_html_()) to get the relevant HTML.

This PR makes it so objects that implement a _repr_html_ method are renderable when they appear as a child of a Tag/TagList. Also note that, using this approach, we can statically and dynamically render these objects:

import folium

from shiny import App, render, ui

app_ui = ui.page_fixed(
    "Map 1",
    folium.Map(location=(45.5236, -122.6750)),
    ui.br(),
    "Map 2",
    ui.output_ui("map")
)

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

app = App(app_ui, server)