with this code type in text into the textbox and hit the button. I’d like to add items to “a” list and keep them there.
Is there a better way to do this?
import functools
import streamlit as st
st.title('interactivity problems saving to lists')
st.subheader('a is a list')
st.subheader("saving things to a container wasn't any better")
a = list()
def fn(new_thing):
st.write(new_thing)
a.append('static item in the function')
a.append(new_thing)
with col1:
st.write(a)
ta = st.text_area('Enter a thing to save, it should output below')
st.button("append to list", on_click=functools.partial(fn, ta))
a.append("static item almost at the end")
st.write("the list is: ", a)
col1, col2, = st.columns(2)
Hi @jlangley, the best option for keeping track of something like that, that should persist and get added to even if the script gets rerun, is to use st.session_stateSession State - Streamlit Docs
Here’s an example of what that might look like:
import streamlit as st
st.title("interactivity problems saving to lists")
st.subheader("a is a list")
st.subheader("saving things to a container wasn't any better")
if "mylist" not in st.session_state:
st.session_state["mylist"] = []
def fn(new_thing):
st.write(new_thing)
st.session_state["mylist"].append("static item in the function")
st.session_state["mylist"].append(new_thing)
with col1:
st.write(st.session_state["mylist"])
ta = st.text_area("Enter a thing to save, it should output below")
st.button("append to list", on_click=fn, args=(ta,))
st.session_state["mylist"].append("static item almost at the end")
st.write("the list is: ", st.session_state["mylist"])
(
col1,
col2,
) = st.columns(2)
A few things to note:
The functools.partial is a clever solution, but you can also just pass args to your button so that it will pass that argument to the function for you
All of those append calls that aren’t inside of the onclick will get run every time the script runs, so you may end up with more duplicated rows being added to your list than you were hoping for.
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.