I am using streamlit extension streamlit-aggrid and I have a selectable table with row groups. I am not able to gather all details of the rows selected when selecting a grouped row.
Here is a runnable issue_example.py
:
import streamlit as st
from st_aggrid import AgGrid, ColumnsAutoSizeMode
import pandas as pd
#_ Input data
df = pd.DataFrame({
'Category': ['Fruit', 'Fruit', 'Vegetable', 'Vegetable'],
'Items': ['Apple', 'Banana', 'Tomato', 'Carrots'],
'Price': [1.04, 1.15, 1.74, 1.5]})
#_ Ag Grid table
st.markdown('# Issue: how to get group selection?')
st.write("Try selecting an aggregate, and then an atomic record")
grid_options = {
"columnDefs": [
{"field": "Category", "rowGroup": True, "hide": True},
{"field": "Items"},
{"field": "Price"},
],
"rowSelection": "single",
}
#_ Playing with response
response = AgGrid(
df,
grid_options,
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW,
)
if response['selected_rows']:
selection=response['selected_rows'][0]
st.write("Current selection is provided as a nested dictionary, requesting `['selected_rows'][0]` value of AgGrid response:")
st.write(selection)
if "Items" in selection:
st.markdown('#### Good!')
Category = selection['Category']
Item = selection['Items']
Price = selection['Price']
st.write(f"We know everything about current selection: you picked a `{Category}` called `{Item}`, with price `{Price}`!")
else:
st.markdown('#### Bad!')
nodeId = response['selected_rows'][0]['_selectedRowNodeInfo']['nodeId']
st.write(f"All we know is that a node with Id `{nodeId}` is selected.\n\r How do we know if you're looking for a `Fruit` or a `Vegetable`?")
when running the above with streamlit run issue_example.py
and selecting the Fruit
group row, the response of AgGrid is a dictionary that contains no information about the row details in Fruit
group. It does not even tell me that I selected Fruit
. I need to have a way to know that when I am selecting Fruit and that the selected rows inside are Apple
and Banana
.
See screenshot for the running streamlit app:
This is just a hack on how to get the category selected. There is a hint on "nodeId":"row-group-0"
.
If you select fruit, you will get row-group-0 and if you select vegetable you will get row-group-1.
Now take the unique values on the Category column. You will get a numpy array which supports an index to get the value.
To get which category just use the index that is derived from nodeId.
Use your code and modify after the else.
else:
st.markdown('#### Bad!')
nodeId = response['selected_rows'][0]['_selectedRowNodeInfo']['nodeId']
st.write(f"All we know is that a node with Id `{nodeId}` is selected.\n\r How do we know if you're looking for a `Fruit` or a `Vegetable`?")
# Get the unique category.
unicat = df['Category'].unique()
# Get the node id and convert to int.
nid = response['selected_rows'][0]['_selectedRowNodeInfo']['nodeId']
nid = int(nid[-1]) # take the last char and convert to int as index to unicat
# Show selected description based on unicat.
selected = unicat[nid]
st.write(f'selected: {selected}')
If you need to get the items, based on the selected category, just use the df.
sel_items = df.loc[df['Category'] == selected]