Disabled parameter resets the widget value

Hi Streamlit community!

I just noticed that changing the disabled parameter of widgets resets their value. Not sure if this behavior is intended or not, but is there a way to disable a widget while not resetting its value?

Here a code example of the behavior:

import streamlit as st

def toggle_widget():
    if st.session_state.widget_disabled:
        st.session_state.widget_disabled = False
    else:
        st.session_state.widget_disabled = True  

if 'widget_disabled' not in st.session_state:
    st.session_state.widget_disabled = False

widget = st.number_input('Select a number between 1 and 6',min_value=1,max_value=6,value=3,disabled=st.session_state.widget_disabled)

st.button('Toggle widget',on_click=toggle_widget)

st.write(widget)

Thanks in advance!

Hey marduk,

import streamlit as st

def toggle_widget():
    if st.session_state.widget_disabled:
        st.session_state.widget_disabled = False
    else:
        st.session_state.widget_disabled = True  

if 'widget_disabled' not in st.session_state:
    st.session_state.widget_disabled = False

if 'def_val' not in st.session_state:
    defaultValue=3
else:
   defaultValue=st.session_state.def_val

widget = st.number_input('Select a number between 1 and 6',min_value=1,max_value=6,value=defaultValue,key='def_val',disabled=st.session_state.widget_disabled)

st.button('Toggle widget',on_click=toggle_widget)

st.write(widget)
1 Like

Thanks a lot, this works!

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