Hi. I’d like to control some figures using a dataframe for source options. After a bit of exploration I came to the following snippet below (for demo purposes). I’m not entirely happy using the private attribute param._widgets
to add the on_click event. Is there an alternative?
import numpy as np
import pandas as pd
import panel as pn
import param
import plotly.express as px
pn.extension("plotly", "tabulator")
def get_options_df():
return pd.DataFrame(
{
"name": ["beta", "normal", "lognormal"],
"params": ["1, 1, 10000", "0, 1, 10000", "1, 1, 10000"],
}
)
class HistogramParam(param.Parameterized):
options = param.DataFrame(default=get_options_df())
selected = param.Series(default=get_options_df().iloc[0], precedence=-1)
def table_click(self, event):
self.selected = self.options.iloc[event.row]
@param.depends("selected")
def histogram_chart(self):
s = self.selected
data = eval(f"np.random.{s['name']}({s['params']})")
fig = px.histogram(data)
fig.update_layout(autosize=True)
return fig
def view(self):
param = pn.Param(self.param)
param._widgets["options"].on_click(self.table_click)
return param
params = HistogramParam()
pn.Column(params.view(), params.histogram_chart, height=600)