Search code examples
pythonpandascachingmethodsstreamlit

Use return variable from a function or st.session_state


I am learning Streamlit framework along with Python. My question is related to code design. I am using functions to apply transformations to pandas dataframes and returning variables to pass it to another functions or display to the user. On the other hand, i can achieve the same result using st.session_state, assigning the "return" function variable to the st.session_state dictionary which provides access to this variable throughout the code like a global variable. So what´s the right pythonic or streamlit way to pass variables ?

def some_function (dataframe):
   a = do some transformation
   return a

st.dataframe(somefunction())

or

def some_function (dataframe):
   a = do some transformation
   st.session_state['a'] = a

Thanks a lot.


Solution

  • If you have a multipage app, it's probably a good idea to use st.session_state or st.cache_data to avoid to reload your whole data or keeping some information persistent across each rerun.

    There is no real guidelines for that. However, you should read these 2 advanced features: