'Streamlit script gets stuck on @st.cache

I'm using streamlit to create a dashboard to analyse Indian Gov data using this python package, [datagovindia][1]. Everything works fine till I use @st.cache to cache the python object returned from above library.

  1. @st.cache() with no args gets stuck on the very first load.

  2. @st.cache(allow_output_mutation=True) lets the script load for first time but gives this error second time: [![enter image description here][2]][2]

  3. @st.experimental_singleton works same as 2, i.e. works for first time and same error for next load. Here is the code:

    import streamlit as st
    
    @st.cache(suppress_st_warning=True)
    def getDataGovAdaptor(): 
        return DataGovIndia(API_KEY)
    
    datagovin = getDataGovAdaptor()
    result = datagovin.search(description="Wheat",max_results=1,print_results=True)
    

  [1]: https://github.com/addypy/datagovindia
  [2]: https://i.stack.imgur.com/CSGC1.png


Solution 1:[1]

I did try your script using API_KEY = '579b464db66ec23bdd000001cdd3946e44ce4aad7209ff7b23ac571b' and it did not gets stuck it just takes more time around 118s with cache and 18s without cache.

Try to understand the cache idea at https://docs.streamlit.io/library/api-reference/performance/st.cache.

Then compare the function of DataGovIndia()

Note:
=====
Initializing this class may take a few seconds, depending on the speed
of your internet connection.
Initialization performs two key tasks:
    1) Tests server
    - Tests data.gov.in server to check if APIs are functional.
    2) Validates the API-key provided
    - Once validated, the API-key is stored and does not need to be entered again.
    3) Loads latest API meta-data.
    - Downloads and loads data containing the latest details of available APIs.

Can the 1) Tests server be cached? No.

So it is better not to use the cache.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ferdy