Accessing a DataFrame widget selected rows from a parameterized class method

I am trying to write a parameterized class that has a method, which can access the selected rows of a DataFrame widget. If I run the below code in a notebook and then access the DataFrame widget with table_view[1].selection, then I get back the index of the selected row. Is there a way to access the selected indices from a method of the parametrized class?

As an alternative, a method that is able to watch the DataFrame widget for changes and then calculate new values for the table would also be helpful.

Thanks,
Ben

default_df = pd.DataFrame({'names': ['A', 'B'],
                           'v1': [20, 30],
                           'v2': [100, 200],
                           'v3': [480, 360]})

class ReactiveTable(param.Parameterized):
    table = param.DataFrame(default=default_df)
    v1 = param.Number()
    v2 = param.Number()
    v3 = param.Number()
    
    @param.depends('table')
    def update_params_with_selected_row_values(self):
        # Is there a way to access pn.widgets.DataFrame.selection or selected_dataframe here?
        pass

rt = ReactiveTable()
table_view = pn.Column(pn.Row(rt.param.v1, rt.param.v2, rt.param.v3), rt.param.table)
table_view


1 Like

Sorry you didn’t get a quicker reply. You’ll have to create the widget explicitly in the constructor:

default_df = pd.DataFrame({'names': ['A', 'B'],
                           'v1': [20, 30],
                           'v2': [100, 200],
                           'v3': [480, 360]})

class ReactiveTable(param.Parameterized):
    table = param.DataFrame(default=default_df)
    v1 = param.Number()
    v2 = param.Number()
    v3 = param.Number()
    
    def __init__(self, **params):
        super().__init__(**params)
        self.table_widget = pn.Param(rt.param.table)[0]

    @param.depends('table_widget.selection')
    def update_params_with_selected_row_values(self):
        return self.table_widget.selection

rt = ReactiveTable()
table_view = pn.Column(pn.Row(rt.param.v1, rt.param.v2, rt.param.v3), rt.table_widget, rt.update_params_with_selected_row_values)
table_view
1 Like