AgGrid remounting on page rerun

AgGrid table is remounting when components are added before it.

Snipped to recreate the issue:

import uuid
from datetime import datetime

import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder, JsCode


st.write(f"Last run: {datetime.now()}")

if st.button("This will recreate table (most of the time)"):
    st.write({"some text": "more text"})
    st.write({"some text": "more text"})
    st.write({"some text": "more text"})

if st.button("This won't recreate table (usually)"):
    st.write("3")


@st.cache_data
def data():
    return pd.DataFrame(
        [
            {"group": "A", "index": uuid.uuid4().hex, "n": 3},
            {"group": "A", "index": uuid.uuid4().hex, "n": 1},
            {"group": "B", "index": uuid.uuid4().hex, "n": 3},
        ]
    )


grid = GridOptionsBuilder.from_dataframe(data())
grid.configure_default_column(groupable=True, value=True, enableRowGroup=True, aggFunc="sum")
grid.configure_grid_options(getRowId=JsCode("function (params) { return params.data.index; }"))
grid.configure_column("group", rowGroup=True, hide=True)

AgGrid(data(), reload_data=True, allow_unsafe_jscode=True, key="grid", gridOptions=grid.build(), height=500)


if st.button("This won't recreate table"):
    st.write({"some text": "more text"})
    st.write({"some text": "more text"})
    st.write({"some text": "more text"})

When clicking on the first button, a few divs are added and it triggers AgGrid reload which can be seen as it takes some time to load.
(You can also verify it is reloading as any change to a table like sorting it by some column will be lost)

Clicking on the second button which adds only one div in most cases doesn’t trigger the remount.
Same for the to button that will create divs after the table.

Is there a way to force Streamlit to keep the same component instead of remounting?

It is potentially similar to the issue described here: Components keep refreshing/remounting when in a column · Issue #2145 · streamlit/streamlit · GitHub and

Python version: 3.10.8
OS: Windows 10
streamlit==1.22.0
streamlit-aggrid==0.3.4.post3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.