How to use session state to save file uploads and filters

Hi All,

How do I preserve an uploaded file, and the filters applied to it? I have multiple pages, when the user uploads a file, adds the filters they need to the dataframe, and then leaves the page, the page will reset.

How do I ensure the entire page doesn’t reset when leaving/coming back to it?

Here is a code snippet (edited to make it smaller).

Essentially the user is uploading a file, selects a value for combinations, and then applies other filters using the Ag Grid interface.

if uploaded_file is not None:
        
        # To read file as bytes:
        bytes_data = uploaded_file.getvalue()
        #st.write(bytes_data)

        # To convert to a string based IO:
        stringio = StringIO(uploaded_file.getvalue().decode("ISO-8859-1"))
        #st.write(stringio)

        # To read file as string:
        string_data = stringio.read()

        try:
            try:
                df = pd.read_csv(uploaded_file)
            except:
                df = pd.read_excel(uploaded_file)
        except:
            st.error('Error loading file. Please be sure to either upload a CSV or an XLSX')

       user_input = st.slider(
        'Select a range of values',
        0, 7, 3)
       filter_dataframe = contains the new dataframe that the user filtered and range of values from user_input variable

             
        gb = GridOptionsBuilder.from_dataframe(filter_dataframe)
        gb.configure_pagination()
        gridOptions = gb.build()

        AgGrid(combined, gridOptions=gridOptions,columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS)


Thanks for any help or insight on this

Could you explain that further?

Thanks for your reply.

So on page 1 the user will upload the file and alter the dataframe by selecting a value from the scroll bar and filtering the dataframe by using the AgGrid interface.

Then the user goes to page 2 to look at something when they come back to page 1, page 1 is completely reset. The user would have to reupload and reapply the filters again if they weren’t done.

Try this example, not exactly as you described.

The filtered df is backed up in st.session_state.filtered_df.

There are 3 files, main.py, pages/page1.py and pages/upload.py

main.py

import streamlit as st
from st_aggrid import GridOptionsBuilder, AgGrid
import pandas as pd

if 'filtered_df' not in st.session_state:
    st.session_state.filtered_df = pd.DataFrame()

st.write('### main')

if len(st.session_state.filtered_df):
    gb = GridOptionsBuilder.from_dataframe(st.session_state.filtered_df)
    gb.configure_pagination()
    gridOptions = gb.build()
    
    st.write('#### Filtered df')
    AgGrid(st.session_state.filtered_df, gridOptions=gridOptions)

pages/page1.py

import streamlit as st
import pandas as pd

if 'filtered_df' not in st.session_state:
    st.session_state.filtered_df = pd.DataFrame()

st.write('### page 1')

pages/upload.py

import streamlit as st
import pandas as pd
from io import StringIO

if 'filtered_df' not in st.session_state:
    st.session_state.filtered_df = pd.DataFrame()

st.write('### upload')

uploaded_file = st.file_uploader('Choose file')

if uploaded_file:
    # To read file as bytes:
    bytes_data = uploaded_file.getvalue()
    #st.write(bytes_data)

    # To convert to a string based IO:
    stringio = StringIO(uploaded_file.getvalue().decode("ISO-8859-1"))
    #st.write(stringio)

    # To read file as string:
    string_data = stringio.read()

    try:
        try:
            df = pd.read_csv(uploaded_file)
        except:
            df = pd.read_excel(uploaded_file)
    except:
        st.error('Error loading file. Please be sure to either upload a CSV or an XLSX')

    st.write('start df')
    st.dataframe(df)

    user_input = st.slider(
        'Select a range of values',
        0, 7, 3)

    filter_dataframe = df.loc[df.col1 == user_input]
    st.session_state.filtered_df = filter_dataframe  # backup the filtered df

    st.write('filtered df')
    st.dataframe(st.session_state.filtered_df)

sample.csv

col1,col2
3,1
2,2
3,3

Upload page

main page

1 Like

thank you this worked!