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()
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
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.
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.
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
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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.