UI only updated when data isn't cached

I’m having issues with @st.cache_data - when data is in the cache my UI isn’t updated. In the example below, I’ve implemented a really based pagination of a database query (returned as a pandas dataframe).

When I click “next” the page increments, the query runs and the results are displayed. But when I click “previous” the page decrements, the data is in cache but the view doesn’t update.

Am I using @st.cache_data incorrectly?

@st.cache_data(show_spinner=True)
def load_dataframe(page:int=1, page_size:int=10):
    return db.query(limit=page_size, offset=(page-1)*page_size)

def main():

    if 'page' not in st.session_state:
        st.session_state['page'] = 1

    prev_page = st.button('prev')
    if prev_page:
            st.session_state['page'] -= 1

    next_page = st.button('next')
    if next_page:
        st.session_state['page'] += 1

    st.text_area("page", value=str(st.session_state['page']))

    df = load_dataframe(st.session_state['page'])
    st.data_editor(df, hide_index=True)

My fault; there was an issue with the query. When I fixed that it started working!

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