How to update a component with data returned by a data_editor placed below it?

I need to show some statistics about a dataframe at the top of my app.
The dataframe can be edited with a data_editor placed below the statistics pane.
How can I update the statistics component with the edited data returned by the data_editor?

This is a contrived example of what I’m doing:

if "data" not in st.session_state:
    st.session_state["data"] = myDataFrame

st.write(my_statistics_component(data=st.session_state))

updatedDataFrame = st.data_editor(st.session_state["data"], key="editor")
# I can use updatedDataFrame from this line, but not above

I’ve tried assigning the value returned by the editor back into st.session_state[“data”] but it breaks the behaviour of the editor.

I have investigated the on_change callback, but it doesn’t seem a valuable root.

The point is that the updated data is only visible below the editor (after a new re-run). I have investigated also the option of updating the statstics component through its session state, but after reading the following sentence from the docs, I didn’t continue.

Modifying the value of a widget via the Session state API, after instantiating it, is not allowed and will raise a StreamlitAPIException

Do I miss a pattern or am I trying to do something against Streamlit’s architecture?

You need to call your statstics component after calling data_editor, but you can use a container to display it at the top.

if "data" not in st.session_state:
    st.session_state["data"] = myDataFrame

container = st.container()
updatedDataFrame = st.data_editor(st.session_state["data"], key="editor")
with container:
    st.write(my_statistics_component(data=updatedDataFrame))

I was sure I was missing something fundamental. I totally overlooked st.container.
Thanks @Goyo!

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