Interactive Elements, general advice needed

If your app become big you should may be look for the parameterized class

class DatabaseManager(param.Parameterized):
    data_selection = param.Selector(default='Meta-Information', objects=list(db_views.keys()))
    var_of_interest = param.List(default=[])
    df_selected = param.DataFrame()
    
    def __init__(self, **params):
        super().__init__(**params)
        widgets={
            "data_selection": {
                "type": pn.widgets.RadioButtonGroup,
            },
            "var_of_interest": {
                "type": pn.widgets.CrossSelector,
                "name": 'Variables of Interest'
            }
        }
        self._tgw, self._csw = pn.Param(self, parameters=["data_selection", "var_of_interest"],
                                 widgets=widgets, sizing_mode='stretch_width', show_name=False)
        self._on_data_selection_change()
    
    def layout(self):
        return pn.Column(
            self._tgw,
            self.get_additional_information,
            self._csw,
        )
    
    @param.depends("data_selection", watch=True) # no return => watch=True
    def _on_data_selection_change(self):
        self.df_selected = db_views[self.data_selection]
        self._csw.options = list(self.df_selected.columns)
    
    @param.depends("var_of_interest", watch=True) # no return => watch=True
    def _on_var_of_interest_change(self):
        print(self.var_of_interest)
    
    @param.depends("df_selected") # side effect => watch=False
    def get_additional_information(self):
        html_meta = self.get_meta_data(self.df_selected).to_html(classes=['example', 'panel-df'])
        return pn.pane.HTML(html_meta + script, sizing_mode='stretch_width')

    ## get additional information about the relevant tables
    @staticmethod
    def get_meta_data(df):
        ## compute standard dataframe information
        rel_miss = pd.DataFrame({'variable': pd.isna(df).sum().index.values,
                                 'relative_missings': pd.isna(df).sum() / df.shape[0],
                                 'dtype': df.dtypes})

        ## compute number of unique instanes in a variabe for each column. 
        no_of_unique_values = []
        for col in df.columns:
            no_of_unique_values.append({"variable": col, 'nunique': df[col].nunique()})
        no_of_unique_values = pd.DataFrame.from_dict(no_of_unique_values)
        return no_of_unique_values.merge(rel_miss, on='variable')


db = DatabaseManager(data_selection='Chain')
db.layout()
2 Likes