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,

If you’re creating a debugging post, please include the following info:

  1. Are you running your app locally or is it deployed?

Locally.

  1. 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.
  2. Share the link to your app’s public GitHub repository (including a [requirements file] >> NA (App dependencies - Streamlit Docs)).
  3. 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’
)

  1. Share the Streamlit and Python versions.
    Python 3.10.9
    streamlit : Version: 1.34.0
  1. Read your data from a file say csv file.
  2. Load the csv in a dataframe.
  3. Make changes to dataframe.
  4. Save changes to dataframe.
  5. Save the dataframe in a csv file that you use in step 1.
  6. Back to step 1.

If there is page reload or app close/open, it will start at step 1. And since your last changes are saved in a file in step 5, nothing is lost.

You have an initial dataframe loaded, change that to csv loading as in step 1. Save that dataframe in a csv file, and once done, you can now use step 1.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.