So have class TopicGenerator that renders some Streamlit widgets.
Working code: Display button without key shows the selected topic
class TopicGeneration:
def __init__(self, data, inc=0):
......
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()
# WITHOUT KEYS
if st.button("Choose Topic"):
st.write(st.session_state.selected_topic)
Called in main script:
st.session_state["topic_generator"] = TopicGeneration(data)
Result in the UI:
Not working code: Display button with key added
class TopicGeneration:
def __init__(self, data, inc=0):
......
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()
# WITH KEYS
if st.button("Choose Topic", key=key_button_topic):
print("hello")
st.write(st.session_state.selected_topic)
Called in the main script
st.session_state["topic_generator"] = TopicGeneration(data)
Nothing happens when Display is pressed
Third example, to complete the triangle of weirdness. When the button is moved outside of the class, the button with the key works exactly as expected. Is there some different behaviour that affects Streamlit components that:
st.session_state["topic_generator"] = TopicGeneration(data)
if st.button("Choose Topic", key=1234):
print("hello")
st.write(st.session_state.selected_topic)
Result in the UI:
Is there something different about how Streamlit components are treated as within a class scope vs. in the main python scope?