Programmatically push form button?

I have an app with a form and of course a form button.

I would like to be able to “push” the form button when changing a selection from a st.selectbox that is outside the form.

Basically, I need a callback function able to activate the form button.

Is there any way to do it?

I don’t know of a good way to do that which wouldn’t involve some very janky JavaScript to try and simulate a user click.

Since a form prevents the user’s selections from being accessible to Python until submitted, can you possibly not use a form? If you remove your form, yours have free access to programmatically use whatever you had inside it. You can introduce other logic to save and use previous values with a button to update.

import streamlit as st

if "inside" not in st.session_state:
    st.session_state.inside = "A"

def update():
    st.session_state.inside = st.session_state._inside

st.selectbox("Outside", [1,2,3], key="outside", on_change=update)

with st.container(border=True):
    st.selectbox("Inside", ["A", "B", "C"], key="_inside")
    st.button("Submit", on_click=update)

st.write(f"Using {st.session_state.outside} for outside.")
st.write(f"Using {st.session_state.inside} for inside.")

Thanks a lot.

Yes I am going to remove the form and use some session_state.

1 Like

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