Expanders disappears when clicking "Not happy with answer" button

st.title("Chatbot Prototype")

# Initialize session state for query and answer
if 'query' not in st.session_state:
    st.session_state.query = ""
if 'answer' not in st.session_state:
    st.session_state.answer = None

query = st.text_input(label="Was möchtest Du wissen?")

if query and query != st.session_state.query:
    st.session_state.query = query
    with st.spinner('Antwort wird abgerufen...'):
        answer = bot.answer(query)
        st.session_state.answer = answer

if st.session_state.answer:
    st.write(st.session_state.answer)
    st.write("========")
   
    # Displays URLs in an expander
    with st.expander("URLs"):
        for page in bot.websites:
            st.write(page.metadata['source'])
        
    # Displays documents in an expander
    with st.expander("Dokumente"):
        for document in bot.documents:
            st.write(document['metadatas'][0]['pdf_name'])
    
    # Saves bad answer to json 
    if st.button('👎 Antwort war nicht hilfreich'):
        dict_to_json("bad_responses.json", {"date": get_time(), "query": query})

I have the following problem: When I click on the last st.button, the expanders disappear from the streamlit app which I want to avoid. How can I do that? This is probably a newb question but after unsuccessfully trying to find a solution for quite some time, I would highly appreciate any help. Thank you!

:smiley: ou can add the option “expanded” in the expander with a st.session_state.

st.title("Chatbot Prototype")

# Initialize session state for query and answer
if 'query' not in st.session_state:
    st.session_state.query = ""
if 'answer' not in st.session_state:
    st.session_state.answer = None
if 'urls_expander' not in st.session_state:
    st.session_state.urls_expander = False
if 'documents_expander' not in st.session_state:
    st.session_state.documents_expander = False

query = st.text_input(label="Was möchtest Du wissen?")

if query and query != st.session_state.query:
    st.session_state.query = query
    with st.spinner('Antwort wird abgerufen...'):
        answer = bot.answer(query)
        st.session_state.answer = answer

if st.session_state.answer:
    st.write(st.session_state.answer)
    st.markdown("""---""") #It's prettier :D
   
    # Displays URLs in an expander
     with st.expander("URLs", expanded=st.session_state.urls_expander):
        for page in bot.websites:
            st.write(page.metadata['source'])
        st.session_state.urls_expander = True


    # Displays documents in an expander
    with st.expander("Dokumente", expanded=st.session_state.documents_expander):
        for document in bot.documents:
            st.write(document['metadatas'][0]['pdf_name'])
        st.session_state.documents_expander = True
    
    # Saves bad answer to json 
    if st.button('👎 Antwort war nicht hilfreich'):
        dict_to_json("bad_responses.json", {"date": get_time(), "query": query})
        st.success("Danke für Dein Feedback!")

Thank you very much for your help! Unfortunately, your suggestion did not solve my problem. But it made me try again and finally I found a solution :slight_smile: :

st.title("GRACE Chatbot Prototype")

# Initialize session state for query and answer
if 'query' not in st.session_state:
    st.session_state.query = ""
if 'answer' not in st.session_state:
    st.session_state.answer = None
if 'urls' not in st.session_state:
    st.session_state.urls = []
if 'documents' not in st.session_state:
    st.session_state.documents = []

query = st.text_input(label="Was möchtest Du wissen?")

if query and query != st.session_state.query:
    st.session_state.urls = []
    st.session_state.documents = []
    st.session_state.query = query
    with st.spinner('Antwort wird abgerufen...'):
        answer = bot.answer(query)
        st.session_state.answer = answer

        for page in bot.websites:
            st.session_state.urls.append(page.metadata['source'])
        for document in bot.documents:
            st.session_state.documents.append(document['metadatas'][0]['pdf_name'])

if st.session_state.answer:
    st.write(st.session_state.answer)
    st.markdown("""---""")
   
    # Displays URLs in an expander
    with st.expander("URLs"):
        for page in st.session_state.urls:
            st.write(page)
        
    # Displays documents in an expander
    with st.expander("Dokumente"):
        for document in st.session_state.documents:
            st.write(document)
    
    # Saves bad answer to json 
    if st.button('👎 Antwort war nicht hilfreich'):
        dict_to_json("bad_responses.json", {"date": get_time(), 
                                            "query": st.session_state.query, 
                                            "answer": st.session_state.answer})
1 Like

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