Submit click button only works once, how to be able to click multiple times


I’ve made a Streamlit chatbot and I currently have it behind okta. A snippet of my code is below.
What happens:
when you click the ‘Click to login via OKTA’ which is the st_oauth function that does a post request to Okta token verification etc. it brings me to my app which is great. Then I select a table from a drop down and I click submit, which works great also.
However… when I click the Reset button if I have the ‘del st.session_state()’ code this sends me back to the “login to okta”… I want it to just clear the chat

If possible I’d like to get rid of the “Reset” button and just be able to “Submit” a table more then once from the selectbox.

hi = st_oauth('myoauth', 'Click to login via OKTA')

selected_table = st.sidebar.selectbox(
    "Select a table to view the data definition:", options=list(snow_ddl.ddl_dict.keys())

click = st.sidebar.button("Submit", type="primary")

reset_chat = st.sidebar.button("Reset Chat")
# Add a reset button
if reset_chat:
    for key in st.session_state.keys():
        print(f'key in st.session_state {st.session_state}')
        print(f'key in reset chat: {st.session_state.keys()}')
        # = ""
        # st.experimental_rerun()
        # del st.session_state[key] // this doesnt work as it should because it makes me click the login via okta link again above

if click:

    print(f"this is the selected table: {selected_table}")
    key_to_check = selected_table  # users selection of article, social, etc.

    tables_available = [category[key_to_check] for category in table_categories if key_to_check in category]
    tables_description = [category['table_description'] for category in table_categories if
                          key_to_check in category]
    print(f'tables_available...  {tables_available}')

    print(f"st.session_state after click: {st.session_state}")
    if "messages" not in st.session_state:
        # system prompt includes table information, rules, and prompts the LLM to produce
        # a welcome message to the user.
        print(f'begin session_state role system...')
        st.session_state.messages = [{"role": "system", "content": get_system_prompt(tables_available[0],  tables_description)}]

# Prompt for user input and save
if prompt := st.chat_input():
    st.session_state.messages.append({"role": "user", "content": prompt})

If you just want to clear the chat, you should do del st.session_state["messages"]. I suspect you are deleting the entry that st_oauth adds to st.session_state, which is why it’s prompting you to log in again.

1 Like

hi @blackary ! Unfortunately this only deletes the messages visually but it doesnt reset the chat bot. So I can’t click Submit and select another table after I click the Reset, it just re-runs the same table selected earlier.

Also I’ve tried this but nothing happens when I click the Reset button:

def reset_conversation():
    st.session_state.conversation = None
    st.session_state.chat_history = None

st.sidebar.button("Reset Chat", on_click=reset_conversation)

I’ve added a screenshot below, once I select a table from the select st.selectbox and click Submit I’m able to see the chatbot. But when I click the reset chat, I should be able to restart the conversation but the chatbot immediately starts the conversation again with the same table as before.

Here’s a super-simplified version of your script that I think accomplishes the same thing you’re trying to do:

import streamlit as st

tables = {
    "article": ["article", "article_author", "article_category"],
    "social": ["social", "social_author", "social_category"],
    "video": ["video", "video_author", "video_category"],

def reset_chat():
    st.session_state["messages"] = None

selected_table = st.selectbox(
    "Pick a table!", options=tables.keys(), on_change=reset_chat

st.button("Reset Chat", on_click=reset_chat)

if st.button("Submit", type="primary"):
    if not st.session_state.get("messages"):
        st.session_state.messages = [
                "role": "system",
                "content": f"Table {selected_table}. Columns {tables[selected_table]}",

if st.session_state.get("messages"):
    if prompt := st.chat_input():
        st.session_state.messages.append({"role": "user", "content": prompt})

st.write("Session state:", st.session_state)

I find it very helpful to have a line like the last one which just shows the current st.session_state at the end of my script, and I can watch it change as I interact with the app.

1 Like

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