Clean st.cache for specific function in Steamlit

Viewed 387

In streamlit, how can I just clean specific st.cache functions for a part.

I have the following structure:

@st.cache
def get_data_from_csv(folder_name, csv_name):
    # loop through all csv files under that folder_name with name csv_name
    name_list = os.list_dir(folder_path)
    res = pd.DataFrame()
    for n in name_list:
        temp_df = pd.read_csv(name_list + csv_name)
        res = pd.concat([res, temp_df], axis=1, ignore_index = True)
    return res

@st.cache
def other_st_cache_function():
   pass

selected = st.sidebar.radio("some name", ["a", "b", "c"])
if selected == "a":
    df = get_data_from_csv("my_folder", "data.csv")
    st.dataframe(df)
elif selected == "b":
    df = other_st_cache_function()
    st.dataframe(df)

Let's assume after I selected "a", the get_data_from_csv function runs and shows my dataframe. Then the underlying data in "my_folder" changed, and I want to clean only the cached info in get_data_from_csv but not that in other_st_cache_function.

How do I do that?

1 Answers

Streamlit doesn't natively support clearing only specific data from the cache, but you can use session state to similarly store and clear specific pieces of data, e.g.

# Delete a single key-value pair
del st.session_state[key]

# Delete all the items in Session state
for key in st.session_state.keys():
    del st.session_state[key]
Related