Cannot trigger empty on button on_click callback but can do so using straight forward code

I have a following class that renders some content to the page. All the content I write to the page is held inside an st.empty container within the class. I want to create a button that will a) delete the old content and b) regenerate new content. When I try to achieve this with the following code where the button callback is called to clear the value, it doesnt empty the value but it does generate the new topics:

def wrapper():
    new_data = initial_response(seed=randint(0,10000))
    st.session_state["topic_generator"].empty()
    st.session_state["topic_generator"] = TopicGeneration(new_data)

data = initial_response()
st.session_state["topic_generator"] = TopicGeneration(data)

st.button("Regenerate", on_click=wrapper)

When I use this code below however, it behaves as I expects and the old content is removed and replaced with the new one:

st.session_state["topic_generator"] = TopicGeneration(data)

# print(st.session_state)
if st.button("Regenerate"):
    st.session_state["topic_generator"].empty()
    new_data = initial_response(seed=randint(0,10000))
    st.session_state["topic_generator"] = TopicGeneration(new_data)

In my mind, both these code snippets should be equivalent no? (FYI: the empty method call is something I implemented to remove all attached streamlit.components from my container class)

I don’t have your TopicGeneration function to test this out myself, but there is a bit of a bug with st.empty where the backend seems to move ahead faster than the frontend can keep up. If you add time.sleep(.2) immediately after calling the .empty() method, does that change the behavior in the first snippet?

Nope, the old content is still hanging around. Went as far as setting the sleep to 1.0, no difference. Which in my mind doesn’t feel like some racy bug where the empty fails to update the state before the re-render loop, think something is just fundamentally not working with the callbacks? In general, albeit in my very extensive streamlit usage of grand 2 days, I’ve found that when Streamlit state modification within the main scope works as expected but when state mod move into function scopes ie. into functions, things become a little fcky, as per described here as well: Buttons Within a Class Don't Trigger When a Key is Added

Could you post a complete, executable example please? Any simplification to demonstrate just the part that isn’t working as expected would be helpful. I’m trying to stitch together what you’ve posted here and on the new thread, but it’s still incomplete so I don’t have anything to execute yet. :slight_smile:

Yeah here, I have the code here, under the buttons branch of my main repo.

Clicking on “Regenerate Me!!!” results in the following duplicated results:

I think my answer on your other post may be helpful/relevant here as well, but let me know if this is still a separate question.

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