SolaraS
Solara2y ago
Cyrus

ipyaggrid memory leak

Hi, I have an app that uses a few sizeable ipyaggrid grids (one per tab) that are leading to memory leaks (when I replace them with solara.Dataframe's for example, then there is no leak). I am using a AgGrid component with a cleanup function, along the lines below, and it seems to be improving things but wanted to know if there is a better approach, on either python and JS side (using js_post_grid) ?


import ipyaggrid
import plotly.express as px
import solara

df = solara.reactive(px.data.iris())
species = solara.reactive('setosa')

@solara.component
def AgGrid(grid_data, grid_options, **kwargs):
    
    def update_data():
        widget = solara.get_widget(el)
        widget.grid_options = grid_options
        widget.update_grid_data(grid_data)

        def cleanup():
            widget.children = ()  # does this help? 
            widget.layout.close()  # does this help?
            widget.update_grid_data([])  # leads to flickering when cross-filtering grid data
            # widget.close()  # leads to widget permanently disappearing after cross-filtering grid data once
            # gc.collect()   # would forcing garbage collection help?

        return cleanup

    el = ipyaggrid.Grid.element(
        grid_data=grid_data,
        grid_options=grid_options,
        **kwargs,
    )

    solara.use_effect(update_data, [grid_data, grid_options])

@solara.component
def Page():

    grid_options = {
        'columnDefs': [
            {'headerName': 'Species', 'field': 'species', 'enableRowGroup': True},
            {'headerName': 'Sepal Length', 'field': 'sepal_length'},
        ]
    }

    df_filtered = df.value.query(f'species == {species.value!r}')
    solara.Select('Species', value=species, values=['setosa', 'versicolor', 'virginica'])

    AgGrid(grid_data=df_filtered, grid_options=grid_options)

Page()
Was this page helpful?