How to use streamlit server for server-side operations in AG grid

Hi,
We need to load 500k data in ag grid, but our ag grid is not able to load data after 100k rows due to huge data size.
Please let me know if we can use streamlit server to keep the data on server side and use the server-side operation in ag grid.

Please share some sample code or any reference for the same.

Thanks

Hello @Rimpy,

For Streamlit, integrating server-side processing with AG Grid requires a few steps.

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

df = pd.DataFrame(large_dataset)

gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_pagination(enabled=True)
grid_options = gb.build()

def get_grid_data(start_row, end_row, sort_model, filter_model):
    # Implement logic to fetch and process data based on request
    # E.g. query your database with limits, sorting, and filtering
    # Return the processed data and total row count
    return processed_data, total_row_count

grid_response = AgGrid(
    df, 
    gridOptions=grid_options, 
    update_mode=GridUpdateMode.MODEL_CHANGED, 
    allow_unsafe_jscode=True,
)

if grid_response.update_type == GridUpdateMode.MODEL_CHANGED:
    start_row, end_row = grid_response.start_row, grid_response.end_row
    sort_model, filter_model = grid_response.sort_model, grid_response.filter_model
    data, total_rows = get_grid_data(start_row, end_row, sort_model, filter_model)
    AgGrid(data)

Let me know if you might require any further assistance!

Kind Regards,
Sahir

P.S. Lets connect on LinkedIn!

3 Likes

Hi @sahirmaharaj,

Thanks for providing the solution.

But when I am using this code, I am getting below error:
AttributeError: โ€˜AgGridReturnโ€™ object has no attribute โ€˜update_modeโ€™

I am not getting update_type attribute in st_aggrid (from st_aggrid import AgGrid)

Thanks

2 Likes