# Thumbs up /down trigger

**URL:** <https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616>\
**Category:** Using Streamlit\
**Tags:** discussion\
**Created:** [March 13, 2024, 10:30am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616 "2024-03-13T10:30:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jino\_R](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/jino_r/32/50900_2.png) [@Jino\_R](https://discuss.streamlit.io/u/Jino_R)\
**Post date:** [March 13, 2024, 10:30am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/1 "2024-03-13T10:30:22Z")

</div>

Im trying to create a chat engine, i have the thumbs up and down, but after clicking on it, the print statement doesnt activate.

Attaching snippet here -

```
    for text in response.response_gen:
        full_response += text + " "
        time.sleep(0.05)
        message_placeholder.markdown(full_response + "▌")
    message_placeholder.markdown(full_response)
        
    st.session_state.messages.append({"role": "assistant", "content": full_response})

    torch.cuda.empty_cache()

    col1, col2, col3, col4 = st.columns([3,3,0.5,0.5])
    with col3:
        if st.button(":thumbsup:"):
            print("I have been liked")
    with col4:
        if st.button(":thumbsdown:"):
            print("I have been disliked")

```

---

<div class="post-metadata">

**Author:** ![Goyo](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/goyo/32/28219_2.png) [@Goyo](https://discuss.streamlit.io/u/Goyo)\
**Post date:** [March 13, 2024, 11:16am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/2 "2024-03-13T11:16:16Z")

</div>

Your code works as expected for me.

---

<div class="post-metadata">

**Author:** ![Jino\_R](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/jino_r/32/50900_2.png) [@Jino\_R](https://discuss.streamlit.io/u/Jino_R)\
**Post date:** [March 13, 2024, 12:16pm UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/3 "2024-03-13T12:16:46Z")

</div>

i dont know what im doing wrong then, the print statement doesnot print for some reason

---

<div class="post-metadata">

**Author:** ![Goyo](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/goyo/32/28219_2.png) [@Goyo](https://discuss.streamlit.io/u/Goyo)\
**Post date:** [March 13, 2024, 12:35pm UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/4 "2024-03-13T12:35:51Z")

</div>

There seems to be nothing wrong **in the code you posted**.

---

<div class="post-metadata">

**Author:** ![dataprofessor](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/dataprofessor/32/44163_2.png) [@dataprofessor](https://discuss.streamlit.io/u/dataprofessor)\
**Post date:** [March 13, 2024, 1:01pm UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/5 "2024-03-13T13:01:21Z")

</div>

Hi @Jino_R

As you’re using `print()` this would print the statements to the console. To display the statements in the app, you’re need to use `st.write()`.

---

<div class="post-metadata">

**Author:** ![Jino\_R](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/jino_r/32/50900_2.png) [@Jino\_R](https://discuss.streamlit.io/u/Jino_R)\
**Post date:** [March 14, 2024, 4:52am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/6 "2024-03-14T04:52:49Z")

</div>

im pasting more additional code snippet here - @dataprofessor @Goyo  
when the buttons are outside the if condition, it works properly, when inside, it doent activate

```
if prompt := st.chat_input("What can I help you with?"):

    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)
    
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""

        response = query_engine.query(prompt)

        # print('###################################################')
        # for idx, res in enumerate(response.source_nodes):
        # print(f'Matched chunk {idx + 1} {response.source_nodes[idx]}')
        # print('###################################################')

        #response.print_response_stream()

        for text in response.response_gen:
            full_response += text + " "
            time.sleep(0.05)
            message_placeholder.markdown(full_response + "▌")
        message_placeholder.markdown(full_response)
            
        st.session_state.messages.append({"role": "assistant", "content": full_response})

        torch.cuda.empty_cache()
    
    col1, col2, col3, col4 = st.columns([3, 3, 0.5, 0.5])
    with col3:
        if st.button(":thumbsup:"):
            print("I have been liked")
    with col4:
        if st.button(":thumbsdown:"):
            print("I have been disliked")

```

---

<div class="post-metadata">

**Author:** ![Jino\_R](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/jino_r/32/50900_2.png) [@Jino\_R](https://discuss.streamlit.io/u/Jino_R)\
**Post date:** [March 14, 2024, 10:48am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/7 "2024-03-14T10:48:00Z")

</div>

i tried to add this print statement here - it seems like the flow doesnot wait for the button, it just executes and the button prints None instead of the print statements ive triggered

```
with st.chat_message("assistant"):
    message_placeholder = st.empty()
    full_response = ""

    col1, col2, col3, col4 = st.columns([3, 3, 0.5, 0.5])
    with col3:
        if st.button(":thumbsup:"):
            print("I have been liked")
    with col4:
        if st.button(":thumbsdown:"):
            print("I have been disliked")
    
    print("im done")

```

---

<div class="post-metadata">

**Author:** ![dataprofessor](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/dataprofessor/32/44163_2.png) [@dataprofessor](https://discuss.streamlit.io/u/dataprofessor)\
**Post date:** [March 14, 2024, 11:45am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/8 "2024-03-14T11:45:47Z")

</div>

Hi @Jino_R

Have you tried using callback functions for the thumbsup/thumbsdown and see if this could resolve the issue?

More info in the Docs:

> **[Session State - Streamlit Docs](https://docs.streamlit.io/library/api-reference/session-state)**
>
> st.session\_state is a way to share variables between reruns, for each user session.

> **[Add statefulness to apps - Streamlit Docs](https://docs.streamlit.io/library/advanced-features/session-state)**

---

<div class="post-metadata">

**Author:** ![Goyo](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/goyo/32/28219_2.png) [@Goyo](https://discuss.streamlit.io/u/Goyo)\
**Post date:** [March 14, 2024, 11:50am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/9 "2024-03-14T11:50:48Z")

</div>

This doesn’t work because the scripts rerun as soon as you click a button. As the script runs again, `prompt` is no longer truish and the code in the `if` block is not executed.

---

<div class="post-metadata">

**Author:** ![Goyo](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/goyo/32/28219_2.png) [@Goyo](https://discuss.streamlit.io/u/Goyo)\
**Post date:** [March 14, 2024, 11:53am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/10 "2024-03-14T11:53:27Z")

</div>

> [@Jino\_R](#):
>
> it seems like the flow doesnot wait for the button

No, it doesn’t. The script runs top to bottom when a session is started. Then it runs again on each significant user interaction.

---

<div class="post-metadata">

**Author:** ![Jino\_R](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/jino_r/32/50900_2.png) [@Jino\_R](https://discuss.streamlit.io/u/Jino_R)\
**Post date:** [March 15, 2024, 4:30am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/11 "2024-03-15T04:30:12Z")

</div>

let me try to add callbacks

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [September 11, 2024, 4:30am UTC](https://discuss.streamlit.io/t/thumbs-up-down-trigger/64616/12 "2024-09-11T04:30:44Z")

</div>

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