Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveHTML: Enable me to provide bound functions to parameters #5476

Open
MarcSkovMadsen opened this issue Sep 3, 2023 · 0 comments
Open
Labels
reactivehtml type: enhancement Minor feature or improvement to an existing feature
Milestone

Comments

@MarcSkovMadsen
Copy link
Collaborator

I'm on the current main branch of Panel.

I'm trying to help in #5475 by creating a ReactiveHTML pane to wrap the lets-plot package.

I've now made a simple component that accepts a lets-plot PlotSpec. But now I want to give it a bound function that returns the PlotSpec.

I don't know how to do this as I've never seen an example. I think it should either be automatic or well documented.

import panel as pn
import param
from panel.reactive import ReactiveHTML

from lets_plot import __version__ as _lets_plot_version
from lets_plot.plot.core import PlotSpec

class LetsPlotPane(ReactiveHTML):
    object = param.ClassSelector(class_=PlotSpec, precedence=-1)

    _plot_spec_as_dict = param.Dict()


    _template = '<div id="pn-container" style="height:100%;width:100%"></div>'

    __javascript__ = [f"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v{_lets_plot_version}/js-package/distr/lets-plot.min.js"]

    @pn.depends("object", watch=True, on_init=True)
    def _update_config(self):
        if not self.object:
            self._plot_spec_as_dict={}
        else:
            spec =self.object.as_dict()
            if "data" in spec and isinstance(spec["data"], pd.DataFrame):
                spec["data"]=spec["data"].to_dict(orient="list")

            self._plot_spec_as_dict=spec

    _scripts = {
        "render": "self._plot_spec_as_dict()",
        "_plot_spec_as_dict": """
LetsPlot.buildPlotFromProcessedSpecs(data._plot_spec_as_dict, -1, -1, pn_container);
"""
    }
    


from lets_plot import ggplot, geom_line, aes
import pandas as pd

pn.extension()

df = pd.DataFrame({
    'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'y': [2, 4, 1, 8, 5, 7, 6, 3, 9, 10],
})


def create_lets_plot(rng):
    p = (ggplot(df[:rng]) + geom_line(aes(x='x', y='y')))
    return p


plot = create_lets_plot(6)
plot._repr_html_()


slider = pn.widgets.IntSlider(name="range", value=5, start=0, end=len(df))
bound_plot = pn.bind(create_lets_plot, slider)
# bound_plot=LetsPlotPane(object=create_lets_plot(6), width=500, height=200)
pn.Column(slider, bound_plot).servable()

image

You make the bound_plot static by removing the # it displayes - but there is of course no interactivity.

image

@MarcSkovMadsen MarcSkovMadsen added the TRIAGE Default label for untriaged issues label Sep 3, 2023
@MarcSkovMadsen MarcSkovMadsen changed the title Enable me to provide bound functions to parameters of ReactiveHTML components ReactiveHTML: Enable me to provide bound functions to parameters Sep 28, 2023
@MarcSkovMadsen MarcSkovMadsen added type: enhancement Minor feature or improvement to an existing feature reactivehtml and removed TRIAGE Default label for untriaged issues labels Sep 28, 2023
@MarcSkovMadsen MarcSkovMadsen added this to the Wishlist milestone Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reactivehtml type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant