Dynamically created input fields from List/Dict/Set

I’m running my app locally.
Streamlit version: 1.31.0
Python version: 3.10

Project Description: (you can skip this part directly to the Error Description part if you want.)
I’m trying to write a functionality to streamlit that capable of dynamically creating input fields from a given variable/list/dict/set

currently, it looks like this:
usage types:

you can give various information about fields to customize, for example:

Error Description:
The problem is I can’t change the values because it looks at the array first and then sets the default values of input fields from that array, and then tries to set the array’s new values, and after that, it again looks at the initial array’s values.

so I tried to use session state like this:


but unfortunately, the result was the same.

Complete code of related functions:

ready-to-use demo:

Hi @Ege-BULUT

In regards to this:

perhaps you can assign the session state variable to an intermediate variable, then perform the intended operation on that variable.

1 Like

Thanks for the reply :raised_hands:
I finally found the root of the problem and solved it.

The problem is that I was using st.empty()s and random_key=time.time()s in my code to generate input fields.
So for this exact reason, the fields are constantly erased and re-created from scratch.
After bunch of try&errors I finally come up with this solution:

  • No more st_element (st.empty()) input for the function,
    instead of using st_element.<streamlit_component>()
    I convert them into st.<streamlit_component>()

  • No more auto-generated unique keys for each component, I simply erased
    random_key = time.time()and st.component(... key=random_key)

  • instead of
    using as default_val = st.component(value=default_val .... )
    I used an output value for it
    as out_val = st.component(value=default_val ...)

also for this second function

  • No more st.empty() creation
  • instead of directly using iterable (input variable) I use the output_item variable.

and finally I used the st.session_state like this:

now its all functional and working as intended.

working demo :

1 Like

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

Glad to hear that this was resolved, also thanks for the well-written explanation on how this was solved.

1 Like