If you’re wanting more control you really should use watch=True
on your depends decorator:
from panel.viewable import Viewer
class ShowDataFrame(Viewer):
add = param.Action(lambda x:x.add_datapoint())
df = param.DataFrame(default=pd.DataFrame(columns=['x','y']))
def __init__(self,**params):
super().__init__(**params)
self.df_widget = pn.Param(self.param.df)[0]
self._layout = pn.Column(
pn.Param(self,parameters=['add']),
self.df_widget,
self.update_params_with_selected_row_values
)
@pn.depends('df', watch=Truee)
def _update_widget(self):
self.df_widget.value = self.df
self.df_widget.height = int(min(len(self.df)/6,1)*6*self.df_widget.row_height) + self.df_widget.row_height
def __panel__(self):
return self._layout
def add_datapoint(self, event=None):
new_point = dict(zip(['x','y'],np.random.randint(0,10,2)))
self.df = self.df.append(new_point,ignore_index=True)
@pn.depends('df_widget.selection')
def update_params_with_selected_row_values(self):
return self.df_widget.selection
t = ShowDataFrame()
t
Performing side-effects in a ParamMethod
function is really not encouraged, so it’s better to set up the watcher to update the object instead. I’ve also taken the liberty of making your class a so called Viewer
so that you don’t have to manually access the .layout
attribute.