Performance issues with number input widget + session state

Hi Streamlit community!

I need to use a st.number_input() with session state, to preserve the value of widget when disabled. See below a code example:

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=int(3)
else:
   defaultValue=int(st.session_state.def_val)

st.button('Toggle disable on/off',on_click=toggle_widget)

with_session_state = st.number_input('With session state',min_value=1,max_value=10,value=defaultValue,key='def_val',disabled=st.session_state.widget_disabled)

without_session_state = st.number_input('Without session state',min_value=1,max_value=10,value=3,disabled=st.session_state.widget_disabled)

Performance issue detected: If the number input with session state is clicked quickly many times, the value sometimes goes back. This does not happen with number input without session state.

Is there any way to make fix this behavior while still using session state on a number input widget? Thanks in advance.

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