Streamlit Sessions State for Nested Buttons
Asked Answered
C

2

6

I am 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. 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)

I am always getting error:

StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, 
and st.form cannot be set using st.session_state.
Carper answered 27/9, 2022 at 6:42 Comment(0)
C
3

The problem was with how i was using session state for button. Which i was able to understand using this code below. In this way nested button can work together.

import streamlit as st
button1 = st.button('Recommend')
if st.session_state.get('button') != True:

    st.session_state['button'] = button1 # Saved the state

if st.session_state['button'] == True:

    st.write("button1 is True")

    if st.button('Check 2'):

        st.write("Do your logic here")
Carper answered 27/9, 2022 at 8:50 Comment(0)
C
0

Update I am sure why this is the case (dawned on my as soon as I submitted this answer). I am using the key "dialog" elsewhere in my code in st.session_state. In the other case, it's not a widget key, just a state key - so I missed that. Simply changing the name of the key worked for me.

TL;DR if you find your way to this page because you got that particular error, make sure you're not using a key for your widget that's used for something else.

For your case @Tushar, I know your reuse of the key was deliberate to so changing it won't help. But you probably will get help from the documentation here on Buttons to modify or reset other widgets


Original answer from way back - oh 5 minutes ago:

I'm not sure why this is the case but I had this exact error using nothing more than a button with a literal string for the key. The key was not used elsewhere and had nothing to do with session state. In short this fails:

if button("okay", key="dialog"):
    # doesn't matter what I do here - that line throw an exception

with

StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, and st.form cannot be set using st.session_state.

where this works fine:

if button("okay"):
    # no key, no problem?!

I hate giving an answer that I don't explain - but in case you end up here, it might save you some hassle.

Concelebrate answered 19/3, 2024 at 17:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.