What's the correct way to use @st.cache for functions from other files?

Can @st.cache be used in functions from a file that’s going to be imported?

Example:

-- app.py

    from utils import func
    text = func()

-- utils.py

    @st.cache
    def func():
        print("hello")

Will cache work as expected if only the functions in utils.py have the cache decorators?

1 Like

In latest streamlit version, you can use st.cache_data - Streamlit Docs or st.cache_resource - Streamlit Docs

1 Like

Thanks, but this question is about how to use when importing from other files rather than the cache data/resource :smiley:

I managed to figure it out by experimenting though. I can indeed use the decorator on functions in other files that I want to import :smiley:

1 Like

I have the same question, whether we have to define function in the same pages?

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.