@st.cache_ not running properly

Summary

@st.cache_data and @st.cache_resource is not working.
getting error as “MediaFileHandler: Missing file 91eb56392aff5f049d3a2e04520d48df656709f5ba77728596b18199.png” in few instances.

Steps to reproduce

Code snippet:

@st.cache_data
def KMEANS_Distortion(X, Name):

    """
    Visualization of Distortion to select the optimal clusters
    """

    from yellowbrick.cluster import kelbow_visualizer
    from sklearn.cluster import KMeans
    from matplotlib import pyplot
  

    a = kelbow_visualizer(KMeans(),X,  k = (4,30) )
    try: 
        os.mkdir("Output")
        
    except OSError: 
        pass  
    
    pyplot.savefig(f'Output/{Name}.png')

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

At the first time when the code is running the image is generated and saved in the local folder and it is cached. And when below menus or buttons are presses without rerunning same image have to be populated.

Actual behavior:

When below widgets are used every time when the page gets refreshed KMEANS_Distortion function runs again and producing different output.

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:
  • Browser version:

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

P.S. I have the same piece code without any changes in the different directory saved as previous versions which works as expected, unable to understand what is wrong but none of the cache is working in the current version. Please advise what is that i am doing wrong.

Hey @Dkdatascientist,

Thank you for sharing this question and sorry for the late reply!

I’m trying to run your code snippet (thank you for including that!) but unfortunately it’s not runnable as-is. Can you update your post to include a code snippet we can run so we can try to reproduce the error?

Hi Caroline,

thanks for the reply,

I erroneously used st.cache_data.clear() st.cache_resource.clear() in the main function which cleared cache data, and i have rectified it. But i like to know is there anyway i can change the cache location where is getting saved.?

thanks,
Dilip

Hey @Dkdatascientist,

st.cache_data does have a parameter called persist – this might be what you’re looking for?

Optional location to persist cached data to. Passing “disk” (or True) will persist the cached data to the local disk. None (or False) will disable persistence. The default is None.