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)
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.
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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.