Form with session state

I get error:

StreamlitAPIException : Values for st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, and st.form cannot be set using st.session_state.
with st.sidebar.form(key=‘device_s’):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

for

 with st.sidebar.form(key='device_s'):
        acquisition_mode: str = st.selectbox("Select Acquisition Mode:",
                                             ["Single",
                                              "Dark",
                                             ])
        exp_ms = st.number_input("Exposure, ms", key='exp_ms',
                                 value=ss[device_id].exp_ms,
                                 min_value=0.1, max_value=10_000., step=100.,
                                 format="%g")

is here proper way around? I have few objects which have user defined values, which should be remembered. values should be visualized if user switch between objects. I thinked “st.form” good way. but now it look like dead end

  1. Local
  2. Streamlit 1.35.0 and Python 3.11.

I cannot reproduce. After adding the missing parts, your code runs without errors here.

@Goyo
here cleaned full code:

import streamlit as st
from loguru import logger
from streamlit import session_state as ss


def connect():
    ss.list_device_s = ["CCT10 - JETI_PE60"]


class Device:
    def __init__(self, device_id,
                 exp_ms: float = 100.):
        self.device_id: str = device_id
        self.exp_ms: float = exp_ms



def select_settings() -> bool:
    st.write(f"ss.list_device_s {ss.list_device_s}")
    device_id = st.sidebar.selectbox("Select Device:", ss.list_device_s)

    if device_id not in ss:
        logger.info(f"Device ID: {device_id}")
        ss[device_id] = Device(device_id=device_id)

        st.warning(f"device_id: {device_id}, ss[device_id] {ss[device_id]}")

        ss.device_s.append(ss[device_id])  # Append device ID to the list of devices

    st.warning(f"ss[device_id].exp_ms: {ss[device_id].exp_ms}")

    with st.sidebar.form(key='device_s'):
        exp_ms = st.number_input("Exposure, ms", key='exp_ms',
                                 value=ss[device_id].exp_ms,
                                 min_value=0.1, max_value=65_000., step=100.,
                                 format="%g")

        st.error(f"exp_ms: {exp_ms}")

        submit_button = st.form_submit_button(label='✔️ Run Acquisition')

    if submit_button:
        logger.info("Requested list of devices")

    return submit_button


def main():
    if 'is_initialized' not in ss:
        ss.device_s = []  # List to store instances of Device class
        ss.is_initialized = True

    connect()

    if "list_device_s" not in ss:
        logger.debug(f"list devices not in session state")
        st.stop()

    select_setting_on: bool = select_settings()


if __name__ == '__main__':
    main()


You are setting st.session_state.device_s elsewhere in your code, and then using that key for the form. This can be resolved by changing the form key to be anything that’s not already in session state, e.g. st.sidebar.form(key="device_s_form")

Thank you.

1 Like

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