What is the structure of cached data?

I have a function that pulls data from my supabase database.

@st.cache_data
def pull_selected_files(tables, columns):
    data = {}
    dtypes = {}
    for table in tables:
        col_selection = columns.get(table, '*')
        if isinstance(col_selection, list):
            col_selection = ','.join(col_selection)
        st.write(f"Pulling data for table: {table}, columns: {col_selection}")
        query = st.session_state["client"].table(table).select(col_selection)
        response = execute_query(query)
        if response and response.data:
            df = pd.DataFrame(response.data)
            data[table] = df
            dtypes[table] = df.dtypes.to_dict()
            data[table]['source'] = 'database'
    return data, dtypes

I then need to access the data on different pages, but I am struggling to find it. Do I get the data by calling the function again? or do is it stored in a session state?

here is what I have tried:

if "data" not in st.session_state:
    st.warning("No data loaded. Please load data from the database first.")
else:
    data = st.session_state["data"]
    dtypes = st.session_state["dtypes"]

and

st.write(data)

Yes, if the function was called before once, the following calls using the same arguments will return the cached return value, instead of actually reruning the function. The downside is that streamlit instructions contained within the function will be skipped (like the st.write inside pull_selected_files) because the function is no longer actually run, only the return value that was cached is returned.

1 Like

You need to call the function at least once in order to get the data.

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