How to keep a status container in session state?


The last status container will disappear when input a new query. Each time the status container is updated. But I want to hold all the status container on my web.

Steps to reproduce

Code snippet:

class RetrievalHandler(BaseCallbackHandler):
    def __init__(self, container: st.delta_generator.DeltaGenerator):
        self.status = container.status("**正在检索**")

    def on_retriever_start(self, serialized: dict, query: str, **kwargs):
        self.status.update(label=f"**正在检索:** {query}")

    def on_retriever_end(self, documents: List[Document], **kwargs):
        for idx, doc in enumerate(documents):
            source = doc.metadata["研报题目"]
            docid = doc.metadata["研报id"]
            self.status.write(f"**[{idx + 1}]来自研报: “{source}”,研报ID:“{docid}”**")

if user_query := st.chat_input(placeholder="Ask me anything!"):
    with st.chat_message("assistant"):
        retrieval_handler = RetrievalHandler(st.empty())
        stream_handler = StreamHandler(st.empty())
        response = convR_qa(user_query, callbacks=[retrieval_handler, stream_handler])

Expected behavior:

I know session_state can be used to store each variable. But I don’t know how to do that. Thanks for your attention.

Hi @cehaoyang

Yes the session state can be used to store message history, here is a code snippet from the Streamlit Docs (Build conversational apps - Streamlit Docs) that shows how to initialize the session state to store message history.

import streamlit as st

st.title("Echo Bot")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):

Hope this helps!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.