Hi all,
I am trying to get the index of a cell when clicked. I am able to get the row using the return of AgGrid. I am also able to get the row and the column of the clicked cell using JS code but I cannot send the data to the streamlit code.
import streamlit as st
import numpy as np
import pandas as pd
from st_aggrid import AgGrid, DataReturnMode, GridUpdateMode, GridOptionsBuilder, JsCode
@st.cache()
def generate_df():
df = pd.DataFrame(
np.random.randint(0, 100, 30).reshape(-1, 3), columns=list("abc")
)
return df
data = generate_df()
gb = GridOptionsBuilder.from_dataframe(data)
gb.configure_columns(list('abc'), editable=True)
js = JsCode("""
function(e) {
let api = e.api;
let rowIndex = e.rowIndex;
let col = e.column.colId;
let rowNode = api.getDisplayedRowAtIndex(rowIndex);
console.log("column index: " + col + ", row index: " + rowIndex);
};
""")
gb.configure_grid_options(onCellClicked=js)
gb.configure_selection(selection_mode ='single')
go = gb.build()
return_ag = AgGrid(data, gridOptions=go, allow_unsafe_jscode=True, reload_data=False, update_mode=GridUpdateMode.SELECTION_CHANGED)
print(return_ag)
ok maybe this might help: Although I don’t get the index in the example, I am sure it might work the same way…
this part taking from code that I found here…
def aggrid_interactive_table(df: pd.DataFrame):
“”"Creates an st-aggrid interactive table based on a dataframe.
Args:
df (pd.DataFrame]): Source dataframe
Returns:
dict: The selected row
"""
options = GridOptionsBuilder.from_dataframe(
df, enableRowGroup=True, enableValue=True, enablePivot=True
)
#editable = True was removed - need this to be non- editable
options.configure_side_bar()
options.configure_selection("single")
selection = AgGrid(
df,
enable_enterprise_modules=True,
gridOptions=options.build(),
update_mode=GridUpdateMode.SELECTION_CHANGED,
allow_unsafe_jscode=True,
height=200
)
return selection
Then In my code I have the following (I am doing a parent/child form think invoice header/line items)
get the GUID (ID) of the selected row and filter the child row based on that
if selection["selected_rows"]:
c = selection["selected_rows"][0]["ID"]
if c > 0:
st.write(df3[df3['ID'] == c])
What the code is setting c = the “ID Field” i have in the parent Dataframe that is selected. I had to use 0 for the first even thought I am only allowing on selection. and From there I pass it to 2nd dataframe. I have not tried with index, but though this would help. (BTW I started with streamlit yesterday and just learning)
That will simply assign the value in column ‘ID’ of the selected row to c. This will happen regardless whether the clicked column was ‘ID’ or something else, so I’m afraid this won’t help.
I have the same problem. It seems that class AgGridReturn only return row info but col info. Have you solved it?Looking forward to your early reply @dataclass
class AgGridReturn(Mapping):
“”“Class to hold AgGrid call return”“”
data: Union[pd.DataFrame , str] = None
selected_rows: List[Mapping] = field(default_factory=list)
column_state = None
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.