Anything in a module other than your main script won’t be overwritten by reruns or refreshes. So just put the queue in a separate module
Here is an example monkey-patching streamlit
(not that I recommend it, but it makes it easier to demonstrate). The queue will will be shared across sessions and it will maintain its state through reruns and refreshes. The usual caveats about sharing mutable global state in multi-threading code apply. Appending to and popping from a deque
are safe operations according to the docs.
import streamlit as st
from collections import deque
if not hasattr(st, "app_queue"):
st.app_queue = deque()
element = st.text_input("Value")
if st.button("Add to app queue"):
st.app_queue.append(element)
st.info(f"\"{element}\" added to the queue.")
if st.button("Pop from app queue"):
try:
element = st.app_queue.popleft()
except IndexError:
st.error("Queue is empty.")
else:
st.info(f"\"{element}\" popped from the queue.")