Button not registering as clicked when dynamic help string is applied

Hello, I have encountered something I cannot make sense of and was wondering if it is a bug.

I have a button, for submitting information (not a submit button, just regular) with a help string, that changes depending on, whether the button is enabled or disabled. The code disables the button immediately upon change and I would expect it to write “submitted”. However, this is not happening. Here is a snippet:

def disable_button():
    st.session_state.button_disabled = True

def button_help():
    if st.session_state.get('button_disabled', False):
        return "The button is disabled"
    return "The button is enabled"

button = st.button(
        "Get answers", type='primary',
        help=button_help(),
        on_click=disable_button, 
        disabled=st.session_state.get('button_disabled', False)),
        key="my_button"

if button:
    st.write("Submitted")

However, it will work, if I directly set help as a string or if I return the same string in the button_help function no matter, what. i.e. returning “The button is enabled” both when it is enabled and disabled. I also experimented with setting the key for the button, as I thought changing the help string may create an entirely new bytton.

If any of you know what causes this, please, let me know :slight_smile:


The program is running locally and no errors are raised. I am using Python version 3.10.4 and Streamlit version 1.37.1

Hi,
The problem with “on_click”, the code will change => call the function “disable_button” and after rerun. So the state of the button will not be save.
Two options:
Either you can call instead of on_click

if st.button('YOURBUTTON')
    disable_button()
    st.write('submitted')

or you can add it with session_state.


        if 'button_disabled' not in st.session_state:
            st.session_state.button_disabled = False
            st.session_state.button = False
        def toggle_button():
            st.session_state.button_disabled = not st.session_state.button_disabled
            st.session_state.button = not st.session_state.button

        st.session_state.help =f"Button is currently: {'Disabled' if st.session_state.button_disabled else 'Enabled'}"

        button = st.button(
            "Toggle Button",
            on_click=toggle_button,
            disabled=st.session_state.get('button_disabled', False),
            help=st.session_state.help
        )
        if st.session_state.get('button',False):
            st.write("Submitted")

(I got the same problem early this morning :smiley: )
Hope, it’s what you are looking for !

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