Update/manipulate dataframe on button click

For those interested, solved using the response to this post. :partying_face:

Initializing the state is definitely required for messing around with dfs, it would seem.

import pandas as pd
import panel as pn
import param
from panel.viewable import Viewer

data = pd.DataFrame({'first_subset': ['sample1', 'sample1', 'sample1', 'sample2', 'sample2', 'sample2'],
                           'slider_col': [1,2,3,4,5,6],
                           'other': [1,2,3,4,5,6],
                           'other1': [1,2,3,4,5,6]})
options = list(data.first_subset.unique())

# class do_the_things(param.Parameterized):
class do_the_things(Viewer):

    update_output_button = pn.widgets.Button(name='Approve Subset',button_type='primary')
    # set up parameters. those with the suffix _pass are set up in order to be updated in the first callback below.
    input_data = param.DataFrame(default=data,precedence=-1)
    output_data = param.DataFrame(default=data)#,precedence=-1)
    slider_to_filter_subset1 = param.Integer(default=33,bounds=(0,100))
    slider_to_filter_subset2 = param.Integer(default=55,bounds=(0,100))
    subset_data_by_selection = param.Selector(objects=options)
    update_output_button = param.Action(lambda x: x.add_output_data(),label='Approve Interval')
    # add = param.Action(lambda x:x.add_datapoint())
    
    def __init__(self,**params):
        super().__init__(**params)
        self.output_data_widget = pn.Param(self.param.output_data)
        self.input_data_widget = pn.Param(self.param.input_data)
        self._layout = pn.Column(
            pn.Param(self,parameters=['slider_to_filter_subset1','slider_to_filter_subset2','subset_data_by_selection',
                                     'update_output_button']),
            self.output_data_widget,
            self.input_data_widget
            #self.update_params_with_selected_row_values
        )
    
    @pn.depends('output_data',watch=True)
    def _update_output_widget(self):
        self.output_data_widget.value = self.output_data
        self.output_data_widget.height = 5# int(min(len(self.output_data)/6,1)*6*self.output_data_widget.row_height) + self.output_data_widget.row_height

    def __panel__(self):
        return self._layout
    
    def add_output_data(self, event=None):
        data_tosend = self.input_data[self.input_data['first_subset'] == self.subset_data_by_selection]
        data_tosend = data_tosend[(data_tosend.slider_col >= self.slider_to_filter_subset1) & (data_tosend.slider_col < self.slider_to_filter_subset2)]
        if self.output_data is None:
            self.output_data = data_tosend
        else:
            self.output_data = self.output_data.append(data_tosend,ignore_index=True)
            

    
do_things = do_the_things()

pn.extension()

app = pn.Row(do_things.param)
app
1 Like