widgetti / ipyaggrid

Using ag-Grid in Jupyter notebooks.
MIT License
57 stars 15 forks source link

Memory leak in grid creation #61

Open lcorrigan opened 9 months ago

lcorrigan commented 9 months ago

I've only started using ipyaggrid but I'm exited about using it in standalone HTML reports.

However, I've found that creating grid objects seem to have a memory leak. Every time I create a grid into the same variable name, the memory usage increases. Even if I delete the variable and do a garbage collection, it continues to climb. My use-case is a follows: 1) Run a query from a database into a pandas dataframe 2) Pass the dataframe to a function in a .py file (griddler, shown below) that returns a grid from the string representation of the dataframe with a list of columns to pin. I.e.: Not much too it.

I'm using: ipyaggrid 0.5.1 ipykernel 6.25.2 ipython 8.14.0 ipywidgets 8.0.3

I'm having to cache my database query results to tinker around with ipyaggrid because I have to restart the kernel so often to get the RAM back down.

I'm open to workarounds if anyone can help with that.

def griddler(df, pin_list=[], height=400):

    col_defs = ipyaggrid.Util.build_colDefs_for_si_cols(df)

    for d in col_defs:
        if "field" in d.keys():
            if d['field'] in pin_list:
                d['pinned'] = 'left'

    grid_options = {
        'columnDefs' : col_defs,
        'enableSorting': True,
        'enableFilter': True,
        'enableColResize': True,
        'enableRangeSelection': True,
    }

    df_str = pd.DataFrame()

    for c in df.columns:
        df_str[c] = df[c].astype("string")

    g = None

    g = ipyaggrid.Grid(grid_data=df_str,         
            height=400,
            grid_options=grid_options,
            quick_filter=True,
            sync_on_edit=False,
            show_toggle_edit=True,
            export_csv=True,
            theme='ag-theme-blue',
            show_toggle_delete=False,
            columns_fit='auto',
            index=False,
            keep_multiindex=False,
            sync_grid=False)

    return g
lcorrigan commented 9 months ago

Just to help paint the picture on this: image