@st.cache_resource for imported function

Can @st.cache_resource work for a function defined in another module?

All the examples of @st.cache_resource I have seen use the form:

@st.cache_resource
def connect_to_db():
# lots of code here to initialize and configure the database

client=connect_to_db()

Whereas what I really want to do is something like this:

import utils

@st.cache_resource
client=utils.connect_to_db()

Would this work? If not, is there another way to persistently cache β€˜client’?

App is https://uscg-auxiliary-ask.streamlit.app/ running on Community Cloud
GitHub - drew-wks/ASK

Found this approach, which seems to work so far:

import utils

client = st.cache_resource(utils.connect_to_db)

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