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()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.