If you’re creating a debugging post, please include the following info:
- Are you running your app locally or is it deployed?
Locally.
- If your app is deployed:
a. Is it deployed on Community Cloud or another hosting platform? >> NO
b. Share the link to the public deployed app. - Share the link to your app’s public GitHub repository (including a [requirements file] >> NA (App dependencies - Streamlit Docs)).
- Share the full text of the error message (not a screenshot).
How to achieve the functionality in a Streamlit application where modifications made to a DataFrame are preserved even after the page is refreshed or reloaded, I want the modifications keep same on app screen. Not reload or refreshed even I clicked on Reload page option.
For Example of my code I tried but It won’t workout My modifications Vanished and Page reload I don’t want this behaviour , Please help me on this :
import streamlit as st
import pandas as pd
Function to initialize the DataFrame in session state
def initialize_dataframe():
return pd.DataFrame({
‘first name’: [‘John’, ‘Jane’, ‘Jim’],
‘last name’: [‘Doe’, ‘Smith’, ‘Brown’],
‘city’: [‘New York’, ‘Los Angeles’, ‘Chicago’],
‘status’: [‘Option 1’, ‘Option 2’, ‘Option 3’]
})
Initialize session state for DataFrame if it doesn’t exist
if ‘df’ not in st.session_state:
st.session_state.df = initialize_dataframe()
Function to update the DataFrame
def update_dataframe(row, col, value):
st.session_state.df.at[row, col] = value
Display the DataFrame with editable inputs
edited_df = st.session_state.df.copy()
for index, row in edited_df.iterrows():
col1, col2, col3, col4 = st.columns(4)
with col1:
new_first_name = st.text_input(f'First Name {index}', value=row['first name'], key=f'first_name_{index}')
with col2:
new_last_name = st.text_input(f'Last Name {index}', value=row['last name'], key=f'last_name_{index}')
with col3:
new_city = st.text_input(f'City {index}', value=row['city'], key=f'city_{index}')
with col4:
new_status = st.selectbox(f'Status {index}', ['Option 1', 'Option 2', 'Option 3'], index=['Option 1', 'Option 2', 'Option 3'].index(row['status']), key=f'status_{index}')
# Update the DataFrame with new values
update_dataframe(index, 'first name', new_first_name)
update_dataframe(index, 'last name', new_last_name)
update_dataframe(index, 'city', new_city)
update_dataframe(index, 'status', new_status)
Display the updated DataFrame
st.write(“Updated DataFrame:”)
st.dataframe(st.session_state.df)
Provide a way to download the updated DataFrame
csv = st.session_state.df.to_csv(index=False)
st.download_button(
label=“Download CSV”,
data=csv,
file_name=‘updated_dataframe.csv’,
mime=‘text/csv’
)
- Share the Streamlit and Python versions.
Python 3.10.9
streamlit : Version: 1.34.0