A question about manipulating a variable by button

Summary

Add a “history_list” variable to webui_pages/dialogue/dialogue.py in the Langchain-Chatchat repository and on clicking the button at “col[1]” clear its value.

Steps to reproduce

Code snippet:

...
# outside the function
if 'history_list' not in st.session_state:
    st.session_state['history_list'] = []
...
# add something to history_list[]
... 
# 
# The last few lines in the dialogue.py code in the function dialogue_page(): 
    with st.sidebar:

        cols = st.columns(2)
        export_btn = cols[0]
        if cols[1].button(
                "清空对话",
                use_container_width=True,
        ):
            chat_box.reset_history()
            st.experimental_rerun()
            st.session_state['history_list'] = []

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

When I click on this button, the “history_list” should be cleared out.

Actual behavior:

st.session_state['history_list'] = [] 

didn’t work

Requirements file

Consistent with the environment in this repository

Additional information

I’m deploying it to a LAN for multiple people to use, will introducing this variable affect the asynchrony?

Hi @a656418zz

Perhaps you can try using a callback function to clear the history upon click.

def clear_chat_history():
    st.session_state.messages = []
st.button('Clear Chat History', on_click=clear_chat_history)

Hope this helps!

Thank you very much for your help :). I apologise, this doesn’t seem to work in my code. I also found that this function works differently in different positions, this I wonder if this can be used as a kind of inspiration for you?

 with st.sidebar:
      def clear_chat_history():
          if "history_list" in st.session_state:
              print("clean history_list!")
              st.session_state.history_list = []
                
        cols = st.columns(2)
        export_btn = cols[0]
        if cols[1].button(
                "清空对话",
                use_container_width=True,
        ):
            chat_box.reset_history()
            st.experimental_rerun()

Under this position, the value of “history_list” is always empty, whether I press that button or not.

 def clear_chat_history():
     if "history_list" in st.session_state:
         print("clean history_list!")
         st.session_state.history_list = []
                
 with st.sidebar:
        cols = st.columns(2)
        export_btn = cols[0]
        if cols[1].button(
                "清空对话",
                use_container_width=True,
        ):
            chat_box.reset_history()
            st.experimental_rerun()

If it’s outside the “st.sidebar”, then it won’t work when the button is clicked.

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