Streamlit AgGrid
Streamlit-AgGrid is an active Python library that provides a feature-rich implementation of the Ag-Grid JavaScript library as a Streamlit component. It enables developers to create sophisticated, interactive data tables within their Streamlit applications, offering functionalities such as advanced filtering, sorting, inline cell editing, row grouping, and custom cell rendering. The library, currently at version 1.2.1.post2, maintains an active release cadence with frequent updates and bug fixes, typically every few months.
Warnings
- deprecated The `try_to_convert_back_to_original_types` parameter has been deprecated since version 1.2.0. The grid now consistently attempts to maintain proper datatypes when editing data.
- breaking Direct HTML returns in `cellRenderer` functions stopped working with `Ag-Grid` version 29.1.0 (introduced in `streamlit-aggrid` 0.3.4).
- gotcha `streamlit-aggrid` does not officially support Polars DataFrames, which can lead to 'no attribute kind' errors.
- gotcha Performance can degrade significantly with very large datasets (e.g., >1k rows) during extensive edits or sorting, as operations are primarily handled client-side.
- gotcha If `AgGrid` is placed inside an `st.expander` or `st.form`, changes might not persist correctly across reruns without proper handling of Streamlit's session state and form submission.
- breaking An incompatibility issue occurred with Streamlit version 1.43.0, causing `streamlit-aggrid` to be unusable. This was resolved in `streamlit-aggrid` version 1.1.1.
Install
-
pip install streamlit-aggrid pandas streamlit
Imports
- AgGrid
from st_aggrid import AgGrid
- GridOptionsBuilder
from st_aggrid import GridOptionsBuilder
- JsCode
from st_aggrid import JsCode
- AgGrid
from st_aggrid import AgGrid
Quickstart
import streamlit as st
import pandas as pd
from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode, DataReturnMode
st.set_page_config(layout='wide')
# Create a sample DataFrame
data = {
'col1': [1, 2, 3, 4, 5],
'col2': ['A', 'B', 'C', 'D', 'E'],
'col3': [10.1, 20.2, 30.3, 40.4, 50.5],
'col4': [True, False, True, False, True]
}
df = pd.DataFrame(data)
st.subheader('Basic Interactive AgGrid Table')
# Configure grid options
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_column('col1', header_name='Column One', editable=True)
gb.configure_column('col2', header_name='Column Two', editable=True)
gb.configure_selection(selection_mode='multiple', use_checkbox=True)
grid_options = gb.build()
# Display the AgGrid component
grid_response = AgGrid(
df,
gridOptions=grid_options,
data_return_mode=DataReturnMode.AS_INPUT,
update_mode=GridUpdateMode.MODEL_CHANGED,
fit_columns_on_grid_load=True,
height=350,
width='100%',
allow_unsafe_jscode=True, # Required for some advanced customizations like JsCode
enable_enterprise_modules=False # Set to True if using Ag-Grid Enterprise features and have a license
)
st.subheader('Selected Rows')
if grid_response['selected_rows']:
st.write(pd.DataFrame(grid_response['selected_rows']))
st.subheader('Edited Data (if any)')
if grid_response['data'] is not None:
st.write(grid_response['data'])