Hello,
I still got some bad behaviour with the session state (we need to press sometimes twice the reset button)
# Define checkbox keys
checkbox_keys = ['button', 'button2', 'button3', 'button4']
# Initialize session state for checkboxes if not already done
for key in checkbox_keys:
if key not in st.session_state:
st.session_state[key] = False
# Function to reset checkboxes
def reset_checkboxes():
for key in checkbox_keys:
st.session_state[key] = False
# Create a container for checkboxes
with st.container():
# Calculate the number of columns needed
num_cols = 2 # You can change this to adjust the layout
num_rows = (len(checkbox_keys) + num_cols - 1) // num_cols
# Create a grid layout of checkboxes
for row in range(num_rows):
cols = st.columns(num_cols)
for col in range(num_cols):
idx = row * num_cols + col
if idx < len(checkbox_keys):
key = checkbox_keys[idx]
# Display the checkbox and update session state
st.session_state[key] = cols[col].checkbox(key, key=f"checkbox_{key}", value=st.session_state[key])
# Reset button
if st.button('Reset', on_click=reset_checkboxes):
st.rerun()
My current fix basically does a hard reset on every action. Weird behavior is that every first click will not be registered. At least the button works
def reset_checkbox():
"""
Instantiate the key BEFORE YOU CREATE WIDGET. Then assign widget to key so you can reset it anytime
"""
for row, lst in state.matrix.items():
for i in range(len(lst)):
key = f'{row}_{i}'
if key not in state:
state[key] = False
state[key] = False
def reset_matrix():
state.matrix = init_dict()
st.rerun()
@st.experimental_fragment
def seq_part():
"""
Experimental fragment bc we doing a lot of request inside the checkbox so we isolating it as fragment
"""
# Add a button that resets the matrix
if st.button('Cleanup sequencer'):
reset_matrix()
matrix = state.matrix
steps = 16
for row, lst in matrix.items(): # {'row1': [False, False, ..]}
cols = st.columns([1] * steps)
for i in range(steps):
hex_label = f'{i:01X}' # Ensure the label is always a 1-character hexadecimal value
key = f'{row}_{i}'
sequence[i] = cols[i + 1].checkbox(label=hex_label, value=lst[i], key=key, label_visibility='hidden')
state.matrix = matrix
# app part
reset_checkbox()
seq_part()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.