簡體   English   中英

Streamlit 會話 State 用於嵌套按鈕

[英]Streamlit Sessions State for Nested Buttons

我試圖在流光中使用 session state 以具有兩個嵌套按鈕,其中第一個按鈕單擊顯示推薦的電影,第二個提交按鈕提交用戶評論,其中 Z20F35E630DAF44DBFA4C3F68F539 情緒分析正在工作CZ39。 但是嵌套按鈕需要 session state 所以我嘗試了 session Z9ED39E2EA931586B6AEZEF8A 但總是出現錯誤925776 我是流光的初學者,所以我可能遺漏了一些東西。 下面是代碼。

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)

我正在嘗試使此代碼正常工作,但總是出現錯誤 StreamlitAPIException:無法使用 st.session_state 設置 st.button、st.download_button、st.file_uploader 和 st.form 的值。

嗨,我得到了問題的答案,問題在於我如何使用 session state 作為按鈕。 使用下面的代碼我能夠理解。 這樣嵌套的按鈕可以一起工作。

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM