St.cache_resource and st.sessions_state

Can I use st.sessions_state and a function that’s decorated with st.cache_resource ?
I couldn’t find a reference to it.

I tried using creating a function in that matter and I got errors.

Here is an example of using both st.session_state and st.cache_resource in the same program.

import datetime

import streamlit as st

def answer_to_life_the_universe_and_everything():
    return 42

if "date" not in st.session_state: =
answer = answer_to_life_the_universe_and_everything()
    f"The Answer to the Ultimate Question of Life, The Universe, and Everything is **{answer}**."
st.write(f"This session was started on **{}**")

Hello @royassis,

Here’s how you can use st.session_state with a cached function:

import streamlit as st

# Example of a cached function
def compute_expensive_operation(input_value):
    # Simulate an expensive operation
    return input_value * 2

# Initialize session state if not already done
if 'input' not in st.session_state:
    st.session_state['input'] = 10

# Input widget to change the value
st.session_state['input'] = st.number_input('Input Value', value=st.session_state['input'])

# Using the cached function with a session state variable
result = compute_expensive_operation(st.session_state['input'])

st.write(f'Result: {result}')

Hope this helps!

Kind Regards,
Sahir Maharaj
Data Scientist | AI Engineer

P.S. Lets connect on LinkedIn!

➤ Want me to build your solution? Lets chat about how I can assist!
➤ Join my Medium community of 30k readers! Sharing my knowledge about data science and AI
➤ Website:
➤ Email:
➤ 100+ FREE Power BI Themes: Download Now