Unable to get the input fields

Hi all, I am new to this. Yet I am trying to do something. I have created some input fields like text_area, text_input, and some more. I am not using form for some reason. So I created a button that clears the input values( i am using session state and re-initializing the values with empty). The main problem here is I am unable to store the input values after pressing the button. Tried doing so many ways.

for sample idea:

store_the_inputfields=[]
def some_fun():
   global store_the_inputfields
   def clear_fun():
        st.session_state["inputfields_values"] =""

   input fields getting the values from the user
   store_the_inputfields.append(list(st.session_state.keys()))
   button (which clears the values by calling on_click =clear_fun)


def view_values():
   st.write(store_the_inputfields)

some_fun()
view_values()

please correct me if there is any mistake

Hi @Teja_Sruthi_Varanasi, welcome to the forum!

The problem seems to be that the problem you are trying to solve (app state that lasts for the entire session, and doesn’t get lost on reload), is exactly the problem that st.session_state was created to solve. If you clear the session state, the data will be lost. Simply storing the inputs in a list store_the_inputfields won’t last, because the script is rerun after any input. I would recommend switching to a form that clears the input on submit, and storing the submitted data in a dedicated variable in st.session_state.

Ok… thanks for the reply… there the thing is I can’t able to clear the input fields that’s the reason I assigned them to null. And coming to form…

I created a multiselect. Whenever user enters option then a text_input is created for every option. In this way i can’t able to do in form. That’s the reason I haven’t used that. If there is any alternative way please feel free to share.

Of course you can use a form. The application won’t see the updated values until the user clicks the submit button, which may or ay not be what you want.

keys = st.multiselect(
    label="Make a selection",
    options=["Fizz", "Buzz", "FizzBuzz"]
)

values = {}
with st.form("my_form", clear_on_submit=True):
    for key in keys:
        values[key] = st.text_input(label=key)
    st.form_submit_button()

st.write(list(values.values()))

If you want to use each value inmediately after the user types it, assig a key to each text_input so that you can set the value to the empty string programatically.

keys = st.multiselect(
    label="Make a selection",
    options=["Fizz", "Buzz", "FizzBuzz"]
)

if st.button(label="Clear"):
    for key in keys:
        st.session_state[key] = ""

for key in keys:
    st.text_input(label=key, key=key)

st.write([st.session_state[key] for key in keys])

Is there any way to save the session_state values in a list. For every form submission, all the session_state values need to be appended for the list and need to be viewed in view_values() . If anyone know please tell me. I cant able to append the session state values because everytime it is updating. Thank you

Thanks. used forms approach.

I don’t understand what you want, my examples are already adding the values to a list. One of my examples uses a form, the other uses session_state, but none of them uses both.

Both examples create several text_input widgets from the options selected in a multiselect.

Both examples allow the user to clear the input widgets by clicking a button.

Both examples write the list of values entered in the wigdets.

What requirement are you missing?

Yeah. Solved the problem using forms. Thanks. Actually this is an extension of my doubt.
Yeah, we storing and we are printing the options. The thing is, whenever we submit the values it will store them in the session state. For every new form, the values will be changed.

What I am asking is, for each session state values I want to append them to a list.

Example:

store=[]

form 1 values: 
x= examp1
y=examp2
st.session_state["x"] = "examp1"
st.session_state["y"] = "examp2"
store.append(list(st.session_state.items())) 

this is printing the result, the difficulty starts when we submit another form.

store contains only of that form values not the previous forms values. I am asking that. how to store the previous form values and append the current form values with previous form values that in the list

form 2 values:
x= examp3
y=examp4
st.session_state[“x”] = “examp3”
st.session_state[“y”] = “examp4”
store.append(list(st.session_state.items()))

now when we print it should give

 [[["x","examp1"],["y","examp2"]],[["x","examp3"],["y","examp4"]]]

this is what I am asking for.

Thanks

That should work, except that you are appending everything in session_state to the store, which might be much more than what you want. Appending just the values from the form works for me.

store = []

keys = st.multiselect(
    label="Make a selection",
    options=["Fizz", "Buzz", "FizzBuzz"]
)

values = {}
with st.form(key="form1", clear_on_submit=True):
    for key in keys:
        values[key] = st.text_input(label=key)
    st.form_submit_button()

store.append(list(values.items()))

keys = st.multiselect(
    label="Make a selection",
    options=["Foo", "Bar", "FooBar"]
)

values = {}
with st.form(key="form2", clear_on_submit=True):
    for key in keys:
        values[key] = st.text_input(label=key)
    st.form_submit_button()

store.append(list(values.items()))

st.text(store)

ok will try thanks