I want to know that how we can add a button in streamlit ag-grid row that will call a python function with data of that specific row.
Anyone got it?
I’m also looking for same functionality. Tried multiple ways nothing worked
Hi, you can achieve this via streamlit aggrid
import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode
def set_dataframe_grid(df: pd.DataFrame):
grid = GridOptionsBuilder.from_dataframe(df)
grid.configure_selection("single", use_checkbox=False) # this tells streamlit that your dataframe would response to single click in a cell
grid.configure_column(
"ColumnX", headerCheckboxSelection=True, checkboxSelection=True
) # ColumnX is the name of the column in the dataframe where the checkbox will appear.
# since the entire row is returned upon selection, i'd recommend give the name of the first column in your dataframe
grid_options = grid.build()
return AgGrid(
df,
gridOptions=grid_options,
update_mode=GridUpdateMode.MODEL_CHANGED,
allow_unsafe_jscode=True,
)
def on_click_function(selected_row):
if selected_row:
st.write(f"Selected Column is: {selected_row[0]['ColumnX']}")
# Sample data
data = {'ColumnX': [1, 2, 3], 'status': ['In Progress', 'Completed', 'Failed']}
df = pd.DataFrame(data)
# Render grid
grid_response = set_dataframe_grid(df)
# Trigger function on cell click
on_click_function(grid_response['selected_rows'])
```python
Hi, there is now an easier way to do this directly via streamlit
selection = st.dataframe(
df,
on_select='rerun',
selection_mode='single-row',
hide_index=True,
)
you don’t need to use hide_index
, but hiding it makes the df look pretty