Dynamic Vertical Resizing of Stream-Aggrid table on adding filters

Hi Everyone,
I am new to streamlit-aggrid. I have a CSV file which I am loading into a pandas dataframe and creating an Aggrid table on top of it with filters for all the columns present in the dataframe. I have also added other properties to add features like automatic text-wrapping for the cells and paginations, enabled pivot and row grouping etc.

My requirement is I want to dynamically resize the aggrid table size on basis on my filter’s selections as I want to avoid the empty space it is creating at the below in case of small no of rows.
FYI, my filtered data can have 1 as record as well.
Therefore I want to calculate the filter table size on runtime and decide the height of the aggrid table.
Can anyone help me and provide any direction on how can I achieve this.

Please refer to the below code

from st_aggrid import AgGrid
from st_aggrid.shared import GridUpdateMode

import streamlit as st
import json
import pandas as pd

max_height = 500

try:
st.set_page_config(layout=“centered”)
except:
pass

gridOptions = {
“columnDefs”: [
{
“field”: “data_platform”,
“filter”: “agTextColumnFilter”,
“filterParams”: {
“buttons”: [“reset”, “apply”],
},
“minWidth”: 150,
},
{
“field”: “database”,
“filter”: “agTextColumnFilter”,
“filterParams”: {
“buttons”: [“reset”, “apply”],
},
“minWidth”: 150,
},
{
“field”: “tablename”,
“filter”: “agTextColumnFilter”,
“filterParams”: {
“buttons”: [“reset”, “apply”],
},
“minWidth”: 150,
},
{
“field”: “duplicate_tables_list”,
“filter”: “agTextColumnFilter”,
“filterParams”: {
“buttons”: [“reset”, “apply”],
},
“minWidth”: 150,
},
],
“defaultColDef”: {
“flex”: 1,
“minWidth”: 100,
“filter”: True,
“wrapText”: True,
“autoHeight”: True,
“enableRowGroup”: True,
“enablePivot”: True,
“enableValue”: True,
},
“enableRangeSelection”: True,
“pagination”: True,
“rowSelection”: “multiple”,
“suppressRowClickSelection”: True,
“suppressColumnMoveAnimation”: True,
“statusBar”: {
“statusPanels”: [
{“statusPanel”: “agTotalAndFilteredRowCountComponent”},
{“statusPanel”: “agTotalRowCountComponent”},
{“statusPanel”: “agFilteredRowCountComponent”},
{“statusPanel”: “agSelectedRowCountComponent”},
{“statusPanel”: “agAggregationComponent”},
]
},
}

@st.cache_data()
def getData():
import pathlib

path = pathlib.Path(__file__).parent / "b2b_datahub_prod_duplicatetables.csv"
data = pd.read_csv(path)
return data

data = getData()

MIN_HEIGHT = 27
MAX_HEIGHT = 500
ROW_HEIGHT = 30
grid1 = AgGrid(
data,
gridOptions,
update_mode=GridUpdateMode.SELECTION_CHANGED | GridUpdateMode.VALUE_CHANGED | GridUpdateMode.FILTERING_CHANGED,
fit_columns_on_grid_load = True,
height=min(MIN_HEIGHT + len(data) * ROW_HEIGHT, MAX_HEIGHT),
# update_on=,
enable_enterprise_modules=True,
allow_unsafe_jscode=True,

    # key="fix3",
)

Snapshot of the result I am getting on adding filters:
aggrid_dynamic_vertical_sizing-min

Any help would be appreciated.