Positioning out of order st.form widgets

:rotating_light: Before clicking “Create Topic”, please make sure your post includes the following information (otherwise, the post will be locked). :rotating_light:

  1. Are you running your app locally or is it deployed?
    Locally
  2. Share the link to your app’s public GitHub repository (including a requirements file).
    N/A
  3. Share the full text of the error message (not a screenshot).
    N/A
  4. Share the Streamlit and Python versions.
    Streamlit version: 1.30.0
    Python version: 3.11.5

As described in the image, I want ticking a checkbox to not rerun the script (and this requires st.form as I understand).

  1. Using st.form context manager → StreamlitAPIException: With forms, callbacks can only be defined on the st.form_submit_button. Defining callbacks on other widgets inside a form is not allowed. This is because the magnifying glass has a callback.

  2. So I tried without the context manager:

form = st.form(key="query_results", clear_on_submit=True)
form.form_submit_button(label="Submit")
...
item_col1, item_col2, item_col3 = st.columns(
    [0.3, 0.69, 0.01]
)
with item_col1:
    st.button(
        f"🔍",
        key=button_key,
        on_click=update_image_search_options,
    )
with item_col3:
    form.checkbox(
        label="Export to disk",
        key=f"checkbox_{random_id}_{sku_config}",
        value=False,
        label_visibility="collapsed",
    )

but this just moves all the checkboxes together.

Is there an easy solution to this?

Ended up having to wrestle the st.session_state and not use st.form at all.

  1. Create a callback on the on_click of the checkbox to update the st.session_state.checkbox_interaction to True. The intention is to use this to skip any expensive operations (or anything that you don’t want to trigger during the re-run)
  2. At the end of the section change st.session_state.checkbox_interaction to False, in case the user interacts with non-checkbox related widgets. I thought about attaching st.session_state.checkbox_interaction → False to all the non-checkbox related widgets directly, but that would require too many changes, at least the trade off is not worth the effort… for now.

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