justpy-org / justpy

An object oriented high-level Python Web Framework that requires no frontend programming
https://justpy.io
Apache License 2.0
1.22k stars 96 forks source link

jp.AgGrid.load_lod(lod, columnDefs) overrides user-specified self.options.columnDefs #656

Closed ntwrick closed 1 year ago

ntwrick commented 1 year ago

I have to use my own AgGrid wrapper class so that I can merge my columnDefs with the ones provided by jp.AgGrid.load_pandas_frame(df) via jp.AgGrid.load_lod(lod, columnDefs).

I believe this is a bug that should be fixed in the jp.AgGrid.load_lod method.

For example:

column_defs = [
    dict(headerName='ID',   field='id',   hide=True),
    dict(headerName='Type', field='type', width=75),
    dict(headerName='Name', field='name', width=200),
    dict(headerName='Data', field='data', width=215),
    dict(headerName='TTL',  field='ttl',  width=75),
    ]

grid = AgGrid(options=dict(columnDefs=column_defs))

df = pd.DataFrame(domain_records, columns=['id', 'type', 'name', 'data', 'ttl'])
grid.load_pandas_frame(df)
...

class AgGrid(jp.AgGrid):
    '''AgGrid wrapper class for jp.AgGrid 
    - overrides jp.AgGrid.load_lod(lod, columnDefs)
    - allows merge of local columnDefs with user-specified columnDefs
    '''
    def load_lod(self, lod:List[Dict], columnDefs:List[Dict]=None):
        """
        load the given list of dicts
        - merges local columnDefs with self.options.columnDefs allowing user columnDefs

        Args:
            lod(list): a list of dicts to be loaded into the grid
            columnDefs(list): a list of column definitions
        """
        if columnDefs is None:
            # assume lod
            columnDefs = []
            if len(lod) > 0:
                header = lod[0]
                for key, value in header.items():
                    if isinstance(value, int) or isinstance(value, float):
                        col_filter = "agNumberColumnFilter"
                    elif isinstance(value, datetime.datetime) or isinstance(value, datetime.date):
                        col_filter = "agDateColumnFilter"
                    else:
                        col_filter = True  # Use default filter
                    columnDefs.append(Dict({'field': key, "filter": col_filter}))
        if self.options.columnDefs:
            # merge local columnDefs with user-specified self.options.columnDefs
            for local_defs, user_defs in zip(columnDefs, self.options.columnDefs):
                local_defs.update(user_defs)
        self.options.columnDefs = columnDefs
        self.options.rowData = lod
WolfgangFahl commented 1 year ago

see #685 but PR still welcome