Streamlit Sessions State for Nested Buttons and columns

Iam trying to use session state in streamlit to have a two nested button in which first button click shows recommended movies and second submit button submits a review by user on which a model of sentiment analysis is working. But nested buttons require session state so i tried session state but always getting an error. iam a begineer in streamlit so it’s possible that iam missing something. Below is the code.

if 'rec_btn' not in st.session_state:
        st.session_state.rec_btn= False
    
    def callback():
        st.session_state.rec_btn = True
    
    if st.button('RECOMMEND',key = 'rec_btn'):
      col1, col2, col3 = st.columns(3)
     
      with col1:
          st.image(output_images[0])
          st.markdown(output_names[0].upper())
          review = st.text_input(f"How much you liked the movie {output_names[0]}")
          if st.button('submit',on_click=callback):
                review = re.sub('[^a-zA-Z0-9 ]','',review)
                review = tf_idf_vectorizer.transform([review])
                ans = model_sentiment.predict(review)
                if  ans == 0:
                    review = 'Thanks for your positive review'
                else:
                    review = 'Sorry for your negative review'
                st.write(review)

Iam trying to get this code working but always getting errors StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, and st.form cannot be set using st.session_state.

Hey @Tushar_Nautiyal, welcome to our forum!

I think this thread can be useful Button inside button - #7 by gabrielfbueno

Best,
Arnaud

Hi, Yes it did helped me out i was able to fix the issue with the above mentioned thread. Thank you very much for this.

1 Like

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