Trigger Empty Call on a Button Itself

So I have the following streamlit code inside my TopicGenerator class. It shows the topics retrieved from my backend server. I want to be able to regenerate the topics, so to do that I have a button that creates a new TopicGenerator class. I have also created all my elements within self.empy(), so that when the “Regenerate” button is pressed, everything gets removed:

        with self.container.container():
            self.selected_topic = st.radio("Select a topic", self.topics, key=self.id + self.seed)
            if self.selected_topic:
                self.change_topic()
            if st.button("Regenerate"):
                st.session_state["topic_generator"].empty()
                time.sleep(0.4)
                new_data = initial_response(seed=key_button_topic)
                st.session_state["topic_generator"] = TopicGeneration(new_data, inc=self.inc+1)
            if st.button("Choose Topic"):
                st.write(st.session_state)
                st.write(st.session_state.selected_topic)

This gives me an error right away:

image

And another one after (probably when my initial_response method finishes calling, the server takes about 10sec to respond):

Guessing my issue is with trying to delete the button that is itself calling the empty method…?

Hi @kongyiji_Peng

The error that you’re encountering is due to To fix this error, please pass a unique key argument to st.button which indicates that the key parameter in st.button() is being assigned a redundant key name during the iteration.

To resolve this, you’ll have to assign it a unique key name, for example, you can use something like key=f"button_{i}"

Hope this helps!

Best regards,
Chanin