How can I override my parameter selection?

My code is fairly long so I will only include the necessary points.

I am testing my app so it is running locally, with this layout: https://myapp.com:40001/?toggle_selection=shares

Note that there is one parameter called adv_selection. It is optional, and it can be set to either ‘shares’ or ‘percentage’.

My idea is that if the parameter is set to ‘percentage’, the toggle will turn to the ‘on’ position, and the user can choose to turn the toggle off if necessary, and vice versa if the parameter is set to ‘shares’.

However, it looks like my toggle is permanently set to the parameter setting’s choice, and I am unable to switch the toggle to the other position. I believe it’s because I am changing the session_state of “my_toggle”, but I am not sure how I can fix it so that I can override the value of “my_toggle” if there is a parameter already set.

Here is my code:

st.sidebar.caption("You have the option of calculating cost # shares and %. The default is set to using # shares.")


if 'adv_selection' in params:
       if params['adv_selection'] == 'percentage':
                toggle_value = st.session_state["my_toggle"] = True
       elif params['adv_selection'] == 'shares':
                toggle_value = st.session_state["my_toggle"] = False
else:
       toggle_value = st.session_state["my_toggle"] = False

toggle_label = ("Select if you prefer to enter # shares instead" if st.session_state.get("my_toggle", False)
                        else "Select if you prefer to enter %age instead")
    
is_toggle = st.sidebar.toggle(toggle_label, value=toggle_value, key="my_toggle", help='By selecting this toggle, you will be prompted to enter in terms of %age. Leaving this toggle off will prompt you to enter in terms of # shares.')


if toggle_value:   
        st.sidebar.markdown(f"""Calculating for %""")
else:
        st.sidebar.markdown(f"""Calculating for # shares""")

For example, let’s say that the adv_selection parameter is set to ‘percentage’, then my_toggle is set to True and so is the toggle_value variable. Then, is_toggle will be set to the on position and because toggle_value is true, the sidebar will show “Calculating for %”. Vice versa id the adv_selection parameter is set to “shares”.

How can I then override the toggle_value so that it can switch to the other boolean even if the parameter sets the boolean initially?

if 'adv_selection' in params:
       if params['adv_selection'] == 'percentage':
                toggle_value = st.session_state["my_toggle"] = True
       elif params['adv_selection'] == 'shares':
                toggle_value = st.session_state["my_toggle"] = False
else:
       toggle_value = st.session_state["my_toggle"] = False

This snippet executes every time your app reruns. If you try to change the value of the toggle, the app reruns and the value will be changed to match what it pulled from the parameter. I’m not sure about your entire flow, but if you want the toggle to be functional for the user, you need to wrap this in something so that it only initialized the toggle instead of overriding it with every rerun.

if "my_toggle" not in st.session_state:
    if 'adv_selection' in params:
        if params['adv_selection'] == 'percentage':
            toggle_value = st.session_state["my_toggle"] = True
        elif params['adv_selection'] == 'shares':
            toggle_value = st.session_state["my_toggle"] = False
    else:
        toggle_value = st.session_state["my_toggle"] = False