So, as this has come up a few times, here is a really simple example of using a persistent object, between runs, as well as waiting for data entry to be complete before submitting.
This happens to use a persistent list, but you could use any mutable data structure.
import streamlit as st
#create cache object for the "chat"
@st.cache(allow_output_mutation=True)
def Chat():
return []
chat=Chat()
name = st.sidebar.text_input("Name")
message = st.sidebar.text_area("Message")
if st.sidebar.button("Post chat message"):
chat.append((name,message))
if len(chat) > 10:
del(chat[0])
try:
names, messages = zip(*chat)
chat1 = dict(Name = names, Message = messages)
st.table(chat1)
except ValueError:
st.title("Enter your name and message into the sidebar, and post!")
This is a very helpful example, another way of doing this is using @tvst’s SessionState gist
import streamlit as st
import SessionState
state = SessionState.get(chat_list=[])
name = st.sidebar.text_input("Name")
message = st.sidebar.text_area("Message")
if st.sidebar.button("Post chat message"):
state.chat_list.append((name, message))
if len(state.chat_list) > 10:
del (state.chat_list[0])
try:
names, messages = zip(*state.chat_list)
chat1 = dict(Name=names, Message=messages)
st.table(chat1)
except ValueError:
st.title("Enter your name and message into the sidebar, and post!")
Very true. However, this does involve importing a whole new module - my version is literally three lines of code, and makes it pretty clear what’s happening.
The other (dis)advantage about using the SessionState gist is that I think the state may only be shared by the current user.
Using the version I’ve posted, all users connecting to the server see the same object, (which may well not be what you want, but is certainly vital for the “Chat server”!)
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.