Hi, I am learning how to use python/streamlit and making an app that stores data and this part where the user wants to delete all data is giving problems. here is the workflow:
- user clicks on a checkbox to delete data
- user clicks on button to confirm delete - i use a double confirm to ensure no one accidentally deletes all data
- all data is deleted - the page should automatically refresh to show ‘no data’
- The checkbox is reset to ‘unchecked’ so the user can come back here and delete data again if they reimport anything.
I know we can have an st.rerun() to automatically refresh if user deletes the data, but streamlit doesn’t recommend having a rerun inside a button condition. For a good reason too, I saw that if I use a rerun inside there, no matter what if/else condition i put it in, it always keeps refreshing the page. Problem is, without a rerun, I don’t see how I can refresh the page to show no data. If i don’t use it, it says all data cleared but still shows all the data in all pages. I have tried different variables, session_state variables, etc. but somehow it’s either one of the two problems.
Another problem I have seen is if I revert the checkbox to unchecked, it just doesn’t delete my data. The behavior I see is it clears out my data if I click on the button twice (even though the page doesn’t refresh and my table is still populated), and the second time it gives me this error: streamlit.errors.StreamlitAPIException: st.session_state.delAllCheck
cannot be modified after the widget with key delAllCheck
is instantiated.
The third time I click on the confirm delete button, it actually refreshes the page and the error goes away.
This is the part of main.py
with col1:
st.session_state.confirm_delete = st.checkbox("Delete all history", value=False, key="delAllCheck")
butclick = st.button("Confirm")
if butclick and st.session_state.get('confirm_delete', True):
df = clear_all_history()
#st.session_state.delAllCheck = False
st.success("All history has been cleared")
#st.rerun()
st.session_state.confirm_delete = False
st.session_state.delAllCheck = False
else:
st.session_state.confirm_delete = False
st.warning("Check the box and hit delete. Warning, will delete all data!")
This is my sub call from utils.py that deletes all data:
def clear_all_history():
"""Clear all history"""
df = pd.DataFrame(columns=['date', 'name, 'customergroup'])
df.to_csv('data/customers.csv', index=False)
#print("All history cleared")
Is there a better way to design this workflow or maybe use a different function to achieve what I want?