Time.sleep disrupts button nesting even with session state

without adding time.sleep() inside the buttons, it works just fine.
However, including time.sleep() within the buttons disrupts the intended behavior of nested buttons working independently.
This causes an issue where toggling a nested button triggers its parent button as well, compromising the individual functionality of the nested buttons.

if "father" not in st.session_state:
    st.session_state["father"] = False

if "child" not in st.session_state:
    st.session_state["child"] = False

if "grandchild" not in st.session_state:
    st.session_state["grandchild"] = False
    st.session_state["child"] = False

if "donkey" not in st.session_state:
    st.session_state["donkey"] = False


if st.button("FATHER"):
    st.session_state["father"] = not st.session_state["father"]


if st.session_state["father"]:
    st.write("FATHER HAS APPEARED")
    if st.button("CHILD"):
        st.session_state["child"] = not st.session_state["child"]

if st.session_state["father"] and st.session_state["child"]:
    # time.sleep(2)

    st.write("CHILD HAS APPEARED")

    if st.button("GRANDCHILD"):
        st.session_state["grandchild"] = not st.session_state["grandchild"]

if (
    st.session_state["father"]
    and st.session_state["child"]
    and st.session_state["grandchild"]
):
    # time.sleep(2)
    st.write("GRANDCHILD HAS APPEARED")

    if st.button("DONKEY"):
        st.session_state["donkey"] = not st.session_state["donkey"]


if (
    st.session_state["father"]
    and st.session_state["child"]
    and st.session_state["grandchild"]
    and st.session_state["donkey"]
):
    st.write("GO BACK!")


st.write(
    f"""
    ## Session state:
    {st.session_state["father"]=}
    
    {st.session_state["child"]=}
    
    {st.session_state["grandchild"]=}    
    
    {st.session_state["donkey"]=}
    """
)

What version of Streamlit are you using?

My first thought is this bug (my comments there cover two separate issues with the original post): Infinite loop after breaking · Issue #6494 · streamlit/streamlit · GitHub
And possibly some relation to fastRerun:
Button / `st.session_state` bug introduced with 1.20? · Issue #6643 · streamlit/streamlit · GitHub

Can you try Streamlit 1.19?

(I don’t think it’s time.sleep so much as whether or not the page has completely loaded before a button is clicked.)

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