Changing the value of an st.toggle based of another st.toggle

for name in mp.NAMES:
    count += 3
    # Needs to be redeclared to line everything in same line
    col_1, col_2, col_3= st.columns(3)
    with col_1:
        st.markdown(f'<p style=\'text-align: left; color: black;\'>{mp.name_mapping(name)["label"]}</p>',
                    unsafe_allow_html=True)
    with col_2:
        dynamic_toggle_1[model] = st.toggle('Op 1', key=count)
        dynamic_toggle_2[model] = st.toggle('Op 2', key=count + 1)

    with col_3:
        if dynamic_toggle_2[model]:
            dynamic_toggle_3[model] = st.toggle('Op3', key=count + 2, disabled=False)
        else:
            dynamic_toggle_3[model] = st.toggle('Op3', key=count + 2, disabled=True)

I’m trying to make the toggle buttons on column 3 to false when I toggle the button 2 on the column 2 to false.

I already was able to disable it but I wanted to change it to false too and I can’t figure how to do this.

Any one able to point me to the right direction?

Thanks in advance

Hi @jpdoliveira, welcome to the community! :wave: :smile:

You can condition the value of the column 3 toggle on the column 2 toggle using session state and st.toggle’s on_change callback.

Add a callback on the column 2 toggle such that every time its state changes, we check if it is False and, if so, set the column 3 toggle’s value to False using the latter’s key.

Here’s a minimal reproducible example demonstrating this behavior:

import streamlit as st

def make_toggle2_false():
    # When toggle 1 is False, set toggle 2 to False
    if not st.session_state.t1:
        st.session_state.t2 = False

col1, col2 = st.columns(2)

col1.toggle("Toggle 1", key="t1", on_change=make_toggle2_false)

col2.toggle("Toggle 2", key="t2")

dependent-toggles

Hope this helps! :balloon:

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