- I am building a
streamlit
app that will be used by multiple users and I cant allow cross talk between users so I am not using any caching anywhere. - App starts by uploading pdf file and parsing them into dataframe which gets saved into dictionary for keeping variable dataframe names.
Issue is when 1st file is already uploaded, processed into a dataframe, saved in dictionary and then if user uploads another file then it completely runs again and overwrites everything and I loose previous data.
I am not able to retain the 1st processed dataframe or dictionary. I cant cache the data as to not allow cross talk. So how should I over come this issue ?
Below is the sample structure that I am following:
# upload pdf file (uploading 1 file at a time currently)
st.session_state.uploaded_file = st.file_uploader('Choose your **.pdf** file to upload', type="pdf")
if st.session_state.uploaded_file:
# starting counter to keep track of file counter
st.session_state.counter = 1
# Some function to Parse uploaded pdf file that I am importing from another file
parsing_function()
st.success('Parsing is Complete')
# saving dataframe after processing
df = some_data_processing()
# Allowing user to edit dataframe and save it
st.session_state.user_df = st.data_editor(st.session_state.df, num_rows="dynamic", key = st.session_state.counter)
# creating blank dictionary
st.session_state.dict_of_df = {}
# can also use filename instead of counter to keep distinct names
st.session_state.key_name = 'user_df_' + str(st.session_state.counter)
# saving this edited user_df into dictionary
st.session_state.dict_of_df[st.session_state.key_name] = st.session_state.user_df.copy(deep=True)
# creating list of dataframes that starts with user_df*
# st.session_state.users_df_list = [key for key in st.session_state.keys() if key.startswith('user_df')]
st.session_state.users_df_list = list(st.session_state.dict_of_df.keys())
st.session_state.selected_df_list = st.multiselect("User Created Dataframes",
st.session_state.users_df_list,
default= st.session_state.users_df_list)
st.session_state.counter +=1
This is how it looks:
Now after all of this if I upload a new pdf file everything gets overwritten. What I want to retain user_df and keep adding to st.session_state.users_df_list and that is what I am not able to think of.
Appreciate any help that can guide me on how to retain above created user_dfs when upload subsequent files.
I have also uploaded this Query in Stackoverflow post