Checkbox status after click

for button_info in button_info_list:
        button_title = button_info.get("title", "")
        payload = button_info.get("payload", "").lstrip("/")        
        st.session_state.chkvalue = st.checkbox(label=button_title,  
                              key=payload,on_change=btnclick, args=[payload])
       if st.session_state.chkvalue :
           some code.....

please some one help .i want to process code after checkbox clicked…but i never get st.session_state.chkvalue value true. Thanks

The simplest way to do this is just

checked = st.checkbox(...)

if checked:
    ...

If you’re just using the output right away, there’s no reason to use a session state variable.

If you really want to keep the value in session state, just add a key to the checkbox and it will automatically be added to the session state.

st.checkbox(..., key='chkvalue')

if st.session_state.chkvalue:
    ...
1 Like

thanks for reply …please correct my code.
st.write(“after click”) not processing in the below code. (checked is not TRUE)…may be this code is inside with statement ?

for button_info in button_info_list:
    button_title = button_info.get("title", "")
    payload = button_info.get("payload", "").lstrip("/")

    checked = st.checkbox(label=button_title, key=payload) 

    if checked:
       st.write("after click")

This code works fine for me:

import streamlit as st

button_info_list = [{"title": "Title", "payload": "Foo"}, {"title": "Another title", "payload": "Payload"}]
for button_info in button_info_list:
    button_title = button_info.get("title", "")
    payload = button_info.get("payload", "").lstrip("/")

    checked = st.checkbox(label=button_title, key=payload) 

    if checked:
       st.write("after click")

https://stlite.streamlit.app/?q=bd82dc

Can you share a minimal reproducible script that shows the issue you’re having?

thank you. please check now…when i click any of the button…nothing happens…kindly advice how to change code.

import streamlit
import streamlit as st


from streamlit_chat import message

import streamlit as st
if user_input := st.chat_input("You:"):

    with st.chat_message("user"):
        st.markdown(user_input)
    bot_reply = st.write(user_input)
    assistant_responses = "" 
    if bot_reply:
        assistant_responses = 'test'
        button_info_list = ""
       
    else:
        assistant_responses = "I'm sorry, but I don't understand that input. Please provide a valid request.."
    with st.chat_message("assistant"):

        button_info_list = [{"title": "Title", "payload": "Foo"}, {"title": "Another title", "payload": "Payload"}]
        for button_info in button_info_list:
            button_title = button_info.get("title", "")
            payload = button_info.get("payload", "").lstrip("/")

            checked = st.checkbox(label=button_title, key=payload)

            if checked:
               st.write("after click")

please support. more simple code .how to fix this issue. nothing happens when i click

import streamlit as st
from streamlit_chat import message

if user_input := st.chat_input("You:"):
    st.write(f"User: {user_input}")
        
    bot_reply = "Test bot reply"
    st.write(f"Bot: {bot_reply}")
    button_info_list = [{"title": "Title", "payload": "Foo"}, {"title": "Another title", "payload": "Payload"}]
           
    for button_info in button_info_list:
                button_title = button_info.get("title", "")
                payload = button_info.get("payload", "").lstrip("/")
                if st.button(button_title):
                   st.write("After clicking {button_title")

@B_Ismail Any widget interaction causes the app to be rerun from top to bottom, so clicking the button will rerun the app from the top, and see that (since you already sent the chat message), user_input is empty, so nothing else will appear. If you store the sent messages in st.session_state, you can see the messages and the buttons persist and continue to work.

import streamlit as st

if "messages" not in st.session_state:
    st.session_state["messages"] = []

if user_input := st.chat_input("You:"):
    st.session_state["messages"].append(user_input)

for idx, message in enumerate(st.session_state["messages"]):
    st.write(f"User: {message}")
    bot_reply = "Test bot reply"
    st.write(f"Bot: {bot_reply}")
    button_info_list = [
        {"title": "Title", "payload": "Foo"},
        {"title": "Another title", "payload": "Payload"},
    ]

    for button_info in button_info_list:
        button_title = button_info.get("title", "")
        payload = button_info.get("payload", "").lstrip("/")
        if st.button(button_title, key=f"button_{idx}_{button_title}"):
            st.write(f"After clicking {button_title}")
1 Like

yes.now i understand. Many Thanks.

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