I’m developing a pretty complex Streamlit app. It has many fragments. Development has been going well. But after adding several new features the app is crashing without error messages. Casual debugging with print statements has not been helpful.
Today I realized that a great debug strategy would be to run the function to be debugged independently of the rest of the app … if I could save the session state. I thought that should be easy, but I can’t do it. Not with pickle. Checking the discussion pages, I saw cloudpickle and joblib recommended as alternatives. They don’t work either. I get the same basic error:
streamlit.errors.StreamlitAPIException: _getstate_() is not a valid Streamlit command.
Based on other comments I tried a number of things, including turning the session state into a dictionary, saving dict items separately, and trying to enforce serialization. Nothing helped. Either I got this error or I got an empty dump file.
What data do you have in Session State? If everything is serializable you can just write your own function to export and import the data to something like a csv. (Some loop like for key in st.session_stateto loop through and grab all your keys, values, and types so that you can reconstruct them from saved text.)
I do think everything is serializable! It’s text, numpy arrays (images), and maybe some floats. They are stored in dictionaries and lists. CSVs wouldn’t be easy for the arrays! It seems like you’re telling me that binary objects can’t be saved. Is that right?
I think this is reasonably easy. st.session_state functions like a dictionary. Streamlit logs are downloadable. So simply dump your session state to your log file:
session_state_dump = {key: value for key, value in st.session_state.items()}
logger.info(f"Session snapshot: {session_state_dump}")