I want my users to be able to save and load their session, and I have been trying to do this with pickle. But when the pickled file is re-loaded, the structure of the session state looks correct, but the dataframes are empty or null.
Session state in my case is fairly large, with more than 90 items, some of which are dataframes. These dataframes are not huge, a few kilobytes maybe.
I have been trying to replicate this with a trivial example, but of course it works fine when I do that!
Any ideas on what I could try? (Perhaps something other than pickle?) Using the build-in download button doesn’t work for session state, so it looks like it needs to be serialised in some way.
If you are using Streamlit and encountering issues with pickling and loading session state, you can try the following approaches:
Use Streamlit’s session_state API: Streamlit provides a built-in mechanism called session_state that allows you to store and retrieve data across multiple user sessions. This eliminates the need for pickling and unpickling the entire session state. You can use it like a regular Python dictionary to store your session data.
Here’s an example of how to use session_state:
import streamlit as st
# Set session state
if 'session_state' not in st.session_state:
st.session_state.session_state = {}
# Store data in session state
st.session_state.session_state['data'] = your_data
# Retrieve data from session state
your_data = st.session_state.session_state['data']
By using session_state, you can save and load specific parts of your session state without the need for pickling and unpickling.
Serialize and save dataframes separately: If you still want to serialize and save the session state, you can consider serializing the dataframes separately from the rest of the session state. Instead of pickling the entire session state, pickle the individual dataframes and save them as separate files. When loading the session state, load the dataframes from their respective pickle files and reconstruct the session state as needed.
This approach can help isolate any issues specific to pickling and unpickling dataframes and allow you to handle them separately.
Explore alternative serialization libraries: If pickle is not working as expected for your session state, you can try using alternative serialization libraries like joblib, dill, or cloudpickle. These libraries provide additional features and flexibility for serializing complex objects, including dataframes.
You can install these libraries using pip and try them out as alternatives to pickle.
Remember to handle any exceptions that may occur during the serialization and deserialization process to help identify and resolve any issues with the dataframes or session state.
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.