Hi everyone,
I’m building a Brewery Membership Software app in Streamlit to manage members, membership tiers, and renewal dates for a brewery. I’m stuck on an issue with saving edits in the members table.
What I’m trying to do:
Staff should be able to edit a member’s tier or renewal date in the table and click “Save” to update the data.
What’s happening:
After I click “Save”, the edited values disappear and the table goes back to the original data. [Describe exactly what you see, or paste the full error message here.]
Minimal code to reproduce:
import streamlit as st
import pandas as pd
df = pd.DataFrame({
“member”: [“Alice”, “Bob”],
“tier”: [“Silver”, “Gold”],
“renewal_date”: [“2026-12-01”, “2027-01-15”],
})
edited = st.data_editor(df)
if st.button(“Save”):
df = edited
st.success(“Saved!”)
What I’ve tried:
- Storing the dataframe in st.session_state
- Wrapping the editor in st.form
Environment:
- Streamlit version: 1.64.0
- Python version: [3.x]
- Running on: [local / Community Cloud]
- GitHub repo: [link, if deployed on Community Cloud]
Any pointers on how to persist the edits correctly in my app? Thanks!
Welcome to the Streamlit community, and thanks for the clear question and code!
This is a common stumbling block: when you assign df = edited inside the button block, the assignment is lost on the next rerun because Streamlit reruns your script top-to-bottom and reinitializes df each time. To persist edits, you need to store your DataFrame in st.session_state and always use that as your data source and target for edits.
Here’s a minimal, reproducible pattern that works:
import streamlit as st
import pandas as pd
# Initialize the DataFrame in session_state only once
if "df" not in st.session_state:
st.session_state.df = pd.DataFrame({
"member": ["Alice", "Bob"],
"tier": ["Silver", "Gold"],
"renewal_date": ["2026-12-01", "2027-01-15"],
})
edited = st.data_editor(st.session_state.df, key="editor")
if st.button("Save"):
st.session_state.df = edited
st.success("Saved!")
st.write("Current data:")
st.write(st.session_state.df)
This way, edits are saved to session state and persist across reruns, as recommended in the Streamlit docs and community best practices. If you want to support multipage apps or more advanced workflows, always read and write your DataFrame from st.session_state.
Sources: