Issue on enable and disable

Hi to all,
I wish to try the enabling of buttons in “cascade manner”, but I found some issues.
I wrote the issues as comment, inside the code (row 36 and row 39).
Some one can exaplain to me where is my fault?
The principals issue are the 3° button feel the change status, but the st.session_state don’t sing it.
Te last thing that the code must to do is change the text a row 32, but this doesn’t happen, why?

Thanks.

Stefano G.

import streamlit as st
import time


def change_button_status(button_name: str):
    # st.write(f"{button_name} - {st.session_state[button_name]}")
    st.session_state[button_name] = not st.session_state[button_name]


def sign_end():
    st.session_state["output"] = "Finito!"


st.set_page_config(page_title="Enable Buttons", layout="wide")

if "output" not in st.session_state:
    st.session_state["output"] = "Lancio operazione ....."

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

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

if st.button("Button1", disabled=False):
    change_button_status("Button2")

if st.button("Button2", disabled=not st.session_state["Button2"]):
    change_button_status("Button3")

if st.button("Button3", disabled=not st.session_state["Button3"]):
    st.text(st.session_state["output"])
    with st.spinner('Attendi completamento...'):
        time.sleep(5)
    st.success('Done!')
    # this call don't change the value of the st.text at row 32, why?
    sign_end()

# I must add if, else the status don't change
if st.session_state["Button3"]:
    st.session_state["Button3"] = False

# Print the session state to make it easier to see what's happening
st.write(st.session_state)

1 Like

What value do you expect?

1 Like

That it becomes “Finito!” as setted in the function sign_end()

1 Like

You have that statement (st.text(...)) to show the output. It can never be Finito! because sign_end() is called later.

It does not mean that because,

def sign_end():
    st.session_state["output"] = "Finito!"

is called first in the code before

if "output" not in st.session_state:
    st.session_state["output"] = "Lancio operazione ....."

the value st.session_state["output"] is "Finito!"

The value becomes Finito! if sign_end() is actually called.

1 Like

So, how can I change the text after the sleep?

What is the purpose why you call that:

st.text(st.session_state["output"])

What is the value of st.session_state["output"] to be displayed in text() that you want after the sleep()? before the sleep()?

Hi @ferdy,
I wish to have a description about the process that is running and at the end of process this description must change.
Under there is my code, how can I achieve this goal?

Thanks

import streamlit as st
import time


def change_button_status(button_name: str):
    # st.write(f"{button_name} - {st.session_state[button_name]}")
    st.session_state[button_name] = not st.session_state[button_name]

output = '''In this moment the _service_ processing  
....  
....'''

st.set_page_config(page_title="Enable Buttons", layout="wide")

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

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

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

def sign_end():
    st.session_state["output"] = "Completed!"

if st.button("Button1", disabled=False):
    change_button_status("Button2")

if st.button("Button2", disabled=not st.session_state["Button2"]):
    change_button_status("Button3")

if st.button("Button3", disabled=not st.session_state["Button3"]):
    st.markdown(st.session_state["output"])
    with st.spinner('Wait please .....'):
        time.sleep(5)
    # this call don't change the value of the st.text at row 32, why?
    sign_end()
    st.success('Done!')

# I must add if, else the status don't change
if st.session_state["Button3"]:
    st.session_state["Button3"] = False


# Print the session state to make it easier to see what's happening
st.write(st.session_state)

1 Like

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