def run():
if st.sidebar.button('Run'):
st.session_state['data'] = function_run(arg1,arg2)
with st.form('data_editor_form'):
st.session_state['edited_df'] = st.data_editor(st.session_state['data'])
submit_button = st.form_submit_button('Update')
if submit_button:
st.session_state['edited_df'].to_csv('df.csv')
Expected Behavior: The dataframe should be visible in the browser using data_editor, user should be able to edit some cells as required, then press submit_button to export the edited file as csv.
Current Behavior: The dataframe is visible on the browser, user can edit it but once submit_button is clicked, it doesnt returns true, however the code starts re-running from the beginning include the function - ‘function_run’ and does not export the csv.
Could you please help on how can I achieve the expected behavior?
I have been through other topics on form and data_editor but none of the solution has helped.
It looks like there might be an issue with the way you are using st.session_state. The behavior you’re experiencing could be due to the fact that the st.data_editor creates a new form, and when the form is submitted, the entire app is rerun, including the function_run part.
Here’s a suggested modification to your code:
import streamlit as st
def run():
if st.sidebar.button('Run'):
st.session_state['data'] = function_run(arg1, arg2)
with st.form('data_editor_form'):
st.session_state['edited_df'] = st.data_editor(st.session_state['data'])
submit_button = st.form_submit_button('Update')
if submit_button:
# Process the edited data
edited_data = st.session_state['edited_df'].to_csv(index=False)
# Save the edited data, for example to a CSV file
with open('edited_data.csv', 'w') as f:
f.write(edited_data)
if __name__ == '__main__':
run()