How to use st.dataframe, on_select, callable

I am trying to set up a dataframe with selectable rows using st.dataframe’s on_select and the callable option.

        selection = st.dataframe(
            projects_df,
            use_container_width=True,
            hide_index=True,
            selection_mode="single-row",
            on_select=handle_selection_change
        )

According to the document:

A callable: Streamlit will rerun the app and execute the callable as a callback function before the rest of the app. In this case, st.dataframe will return the selection data as a dictionary.

When it calls my function, how does the return value of st.dataframe get to my function?

You can access it using the app’s session state.

dataframe_callback

Code
import streamlit as st
import pandas as pd

def callback():
    with st.sidebar:
        st.write("**Callback called**")
        st.write(st.session_state.df)


def main():
    df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

    st.title("Callback example")

    st.dataframe(
        df,
        on_select=callback,
        selection_mode="multi-row",
        key="df",
    )


if __name__ == "__main__":
    main()

How do we pass the selected row index value to the function? Can we do that?

You don’t. You have the selections in st.session_state.df.

Yeah finally used that as per above example, thanks!