posit-dev / py-shinywidgets

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

Support panel widgets #82

Open cpsievert opened 1 year ago

cpsievert commented 1 year ago

Since panel widgets are just bokeh widgets under-the-hood, it seems like it should be possible to support them (an obvious big win for this support would be the Tabulator widget)

We can get most of the way there with the code below, but it's still not clear to me how to properly load Tabulator JS/CSS assets (this is most likely a problem for any bokeh widget "extension" that loads 3rd party JS/CSS).

Here are some relevant threads/PRs that may help with the JS errors that this example generates

https://github.com/holoviz/panel/issues/1525 https://github.com/holoviz/panel/pull/1690

import datetime as dt

import pandas as pd
import panel as pn
from jupyter_bokeh import BokehModel
from shiny import App, ui

from shinywidgets import output_widget, render_widget, bokeh_dependency

app_ui = ui.page_fluid(
    bokeh_dependency(),
    output_widget("my_widget"),
)

def server(input, output, session):
    @output
    @render_widget
    def my_widget():

        df = pd.DataFrame(
            {
                "int": [1, 2, 3],
                "float": [3.14, 6.28, 9.42],
                "str": ["A", "B", "C"],
                "bool": [True, False, True],
                "date": [
                    dt.date(2019, 1, 1),
                    dt.date(2020, 1, 1),
                    dt.date(2020, 1, 10),
                ],
                "datetime": [
                    dt.datetime(2019, 1, 1, 10),
                    dt.datetime(2020, 1, 1, 12),
                    dt.datetime(2020, 1, 10, 13),
                ],
            },
            index=[1, 2, 3],
        )

        w = pn.widgets.Tabulator(df)

        m = BokehModel(w.get_root())

        # TODO: how should Tabulator get loaded?
        print(m._model_module)
        print(m._model_module_version)

        return m

app = App(app_ui, server)