What are the arguments that are passed to the on_change function in st.data_editor (if any)

I am trying to execute a callback to update a session_state variable when a checkbox is selected in a data_editor but i’m unable to pass any arguments into the function. I checked the docs and similarly looked into the source code but was unable to find anything about what arguments the function takes

The function must be defined by you so it can take whatever arguments you want it to take.

Ok, then how do you ensure that the arguments are then passed in correctly? For example

edited_data = st.data_editor(
        results,
        column_config={
            "image_url": st.column_config.ImageColumn(
                "Preview Image", help="Preview Image", width="medium"),
            "pick": st.column_config.CheckboxColumn(
            "Your Pick?",
            help="Pick this drill for the practice")
        },
    on_change=set_picked_drills,
    use_container_width=True,
    )

def set_picked_drills(selected_drills):
    st.session_state.selected_drills = [Drill(**drill) for drill in selected_drills.reset_index().to_dict("records")]

when I do this, i get the following error

Thank you for answering and apologies if this is a silly question, I am very new to python so it’s possible there’s just some language things i’m missing

Ok, I figured out part of it by reading the docs :sweat_smile:

    edited_data = st.data_editor(
        results,
        column_config={
            "image_url": st.column_config.ImageColumn(
                "Preview Image", help="Preview Image", width="medium"),
            "pick": st.column_config.CheckboxColumn(
            "Your Pick?",
            help="Pick this drill for the practice")
        },
    on_change=set_picked_drills,
    kwargs=dict(selected_drill=[]),
    use_container_width=True,
    )

so i’m able to pick up the arg there, but what do i pass to get it to pick up the changed data?

1 Like

My question has now evolved to: is there any way to pass either the changed object or the outputted dataframe to the callback? Things I’ve tried:

assigning a key to the data_editor and attempting to pass the session state variable in: results in an error StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, and st.form cannot be set using st.session_state.

@Goyo any insight?

The answer to your question is no, for data editor and for any other input widget. You set the actual parameters when calling the widget function, before any interaction happening, so there is no way to pass the updated value as a parameter to the callback.

For most input widgets, if you assign a key then you can use session_state to access the new value in the callback. But, as you probably know already, that is not true for data editor. What you have in session_state is not the updated object but a dictionary with the changes.

You can indeed assign a key to the data editor, what you cannot do is using session_state to set the value for that key, as the error message says.

Thank you for your reply.

What you have in session_state is not the updated object but a dictionary with the changes.

Is it possible to access even that? That would do for my purposes. If not I could probably rig up a form and submit the form to capture changes but I’d rather they this first.

Of course it is, that is what I just said.

Sorry, i didn’t understand the first time. Thank you for your help!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.