I have a DF in session state, and I run a function which does some maths and adds the results to new columns to the existing DF.
If I st.write the session state before the function, everything looks right.
If I st.write the state inside the function, after the maths, everything looks right - the DF has the additional columns.
But if I look at the session state after the function has run, the DF is back as it was originally, and nothing can address the new column. This is all in tabs and columns, so I have tried moving the function all over the place, but the same thing happens everywhere: the DF is only modified in the function, and not outside.
Which I am finding very confusing. Any ideas? Let me know if more detail would help! Thanks!
I will try to make a runnable code demo later - given the complexity of the code, it will take a while to distill it down!
But, very pseudo-codey, it goes
def add_column():
results = st.session_state.dataframe["column1"] * maths
st.session_state.dataframe["result"] = results
# debug, look at dataframe
st.session_state.dataframe
# the above line results in the df seen at the top of the screen in the shot below (which is correct)
#main stuff
#draw page
#bring data into session state
#run add_column
add_column()
#debug, look at dataframe
st.session_state.dateframe
# above line displays df at bottom of screenshot below: incorrect, missing columns.
Here is what this is rendering: correct df at top of screen, when asked what it looks like inside the function. Incorrect dataframe at bottom of screen, after function has run.
I suspect there’s a different interaction going on, but am not sure what it might be.
One quick sanity you can do is to do st.write(st.session_state) at strategic points, to make sure you’re not accidentally saving your changes to an object with a different key than the original dataframe.
Yeah, I think you are right, something strange is going on somewhere, but currently I cannot find it.
My workaround (I don’t really understand why it works!) is, at the end of the function which does some maths and puts it into a new column on the DF, is to then duplicate the DF entirely (luckily these things are not huge)… and the virgin DF is seen properly by the rest of the code
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.