Trouble with clearing cache in one button click

Hello,

I am making a sort of guessing game, and I am basically keeping a cache of the random data selected. After 15 incorrect guesses I am giving the option to give up and basically go to the next random selection. My problem I am having is that when I click the give up button, the text bar will be cleared as it should, but then the user would have to click the give up button once again for it to actually go to the next random data item

Code snippet:

def clear_text():
    st.session_state["text"] = ""
    

if st.session_state.wrong >= 15:
    if st.button("Give up?", on_click=clear_text):
        # st.session_state.give_up = True
        st.cache_data.clear()
        st.session_state.wrong = 0
        # st.session_state.give_up = False

Any help in the right direction is appreciated!

Hi @fc_logical

It seems that the callback function was used by st.button() for clearing the st.session_state["text"] and a separate clearing of cache data is applied under the if statement.

Could you try refactoring the code such that the code statements under the if statement is a part of the callback function.

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