My feedback didn't appear

Summary

Hello!
I am making this flashcard game but whenever I clicked the correct or wrong button, I couldn’t see the feedback. Thank you

Steps to reproduce


if show_translation_button_clicked:
    st.session_state.current_flashcard_index += 1

    correct_clicked_button = st.button("Correct")
    wrong_clicked_button = st.button("Wrong")

    current_flashcard3 = current_flashcard["translation"]
    st.write(current_flashcard3)

    if correct_clicked_button:
        print(st.write("You are correct!"))

    if wrong_clicked_button:
        print(st.write("You are wrong"))

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

I expected it to show the feedback whenever I click the button.

Actual behavior:

The feedback did not appear after I clicked the button.

Debug info

  • Streamlit version: Streamlit, version 1.25.0
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:
  • Browser version:

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

Hey @Jun,

Thanks for sharing your question! Can you update your post to include a runnable code snippet so we can try to reproduce the issue you’re seeing?

1 Like

Hello Caroline,

I don’t know how to make a code snippet because I’m just starting to learn python and streamlit. Instead I put here my code.


import streamlit as st


if "current_flashcard_index" not in st.session_state:
    st.session_state.current_flashcard_index = 0
if "user_score" not in st.session_state:
    st.session_state.user_score = 0

flashcards = [
    {"source": "Dog", "translation": "Perro"},
    {"source": "Cat", "translation": "Gato"},
    {"source": "Elephant", "translation": "Elefante"},
    {"source": "Lion", "translation": "Leon"},
    {"source": "Tiger", "translation": "Tigre"},
    {"source": "Dolphin", "translation": "Delfin"},
    {"source": "Bear", "translation": "Oso"},
    {"source": "Monkey", "translation": "Mono"},
    {"source": "Penguin", "translation": "Pinguino"},
    {"source": "Giraffe", "translation": "Jirafa"},
]


st.title("ANIMAL FLASHCARD")

current_flashcard = flashcards[st.session_state.current_flashcard_index]
current_flashcard2 = current_flashcard["source"]
st.write(current_flashcard2)

show_translation_button_clicked = st.button("Show translation")


if show_translation_button_clicked:
    st.session_state.current_flashcard_index += 1

    correct_clicked_button = st.button("Correct")
    wrong_clicked_button = st.button("Wrong")

    current_flashcard3 = current_flashcard["translation"]
    st.write(current_flashcard3)

    if correct_clicked_button:
        print(st.write("You are correct!"))

    if wrong_clicked_button:
        print(st.write("You are wrong"))

Hi @Jun ,
Please try to put st.button("Show translation") in the if condition itself.
I have also faced the same and after some time I try this approach and it worked.

Hey @Jun,
In Streamlit Buttons return True only on the page load right after their click and immediately go back to False.
Simply, If their is a nested button then after clicking th first button the page get’s refreshed automatically. That is the reason why you are not able to see the feedback you want.
For solving this problem you can see the blog of streamlit in which they are explaining this perfectly.

If you want to nested button then you can see the code snippet given below for getting an idea how does the session state works for buttons in Streamlit.

It May help you!!

Thank you! it worked :smile:

1 Like

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