How to get st.dataframe cell value """st.dataframe cell selection event"""

Please review my code for selecting cells in st.dataframe. I obtained this code from the link below; however, it produces an error during the on_change event. Kindly examine the complete code and the error message, and provide the correct code for retrieving the cell value through mouse clicks.
“”“st.dataframe cell selection event”“” in google
code

import streamlit as st
import pandas as pd
import numpy as np

df = pd.DataFrame(
np.random.randn(10, 5),
columns=(‘col %d’ % i for i in range(5))
)

event = st.dataframe(df, key=“data_frame”, on_change=None)

if event[“on_select”]:
selected_rows = event[“selected_rows”]
selected_cols = event[“selected_columns”]

if selected_rows and selected_cols:
    st.write("Selected cell value:")
    st.write(df.iloc[selected_rows[0], df.columns.get_loc(selected_cols[0])])
elif selected_rows:
    st.write("Selected rows:")
    st.write(df.iloc[selected_rows])
elif selected_cols:
     st.write("Selected columns:")
     st.write(df[selected_cols])

error

link

Hi @badar, can you please edit your code so that it’s in a code block?

One issue is that on_change needs to be on_select st.dataframe - Streamlit Docs

dear boss
i change on_change with on_select but it give new error see the error

boss see this line it give error
if event[‘on_select’]: