Clearing Cache in streamlit, python

Viewed 37

I am currently writing a web-application and need to "refresh the page" by clearing the cache. So the user should be able to refresh the page. For this reason I created a button with the following statement in it st.legacy_caching.caching.clear_cache(). However, even I have the most updated version (1.12.0) I get the following error "module 'streamlit' has no attribute 'legacy_caching'"

my code looks like this

@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def load_General():
    engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
    con=engine.connect()
    general_query_raw='''string'''
    General_raw=pd.read_sql(general_query_raw,con)
    
    general_query_cleansed='''string'''
    General_cleansed=pd.read_sql(general_query_cleansed,con)
    return General_raw, General_cleansed


@st.cache(allow_output_mutation=True,suppress_st_warning=True)
def load_Role():
    engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
    con=engine.connect()
    role_query_raw='''string'''
    Role_raw=pd.read_sql(role_query_raw,con)
    
    role_query_cleansed='''string'''
    Role_cleansed=pd.read_sql(role_query_cleansed,con)  
    return Role_raw,Role_cleansed

With the following line(s) I try to clean the cache in order to be able to get the latest data from the DB

def clear_cache():
    st.legacy_caching.caching.clear_cache()

st.sidebar.button("Refresh Program",on_click=clear_cache)

Any Idea what could help me here?

1 Answers

I had come across a similar issue recently, instead of creating a button to clear the cache you can use the st.empty() function that should clear the cache.

Just call as below before calling your defined functions:

load_general = st.empty()
load_role = st.empty()

load_general = load_General()
load_role = load_Role()

https://docs.streamlit.io/library/api-reference/layout/st.empty

Related