Undesirable behavior where all actions trigger an app refresh and the question and answer process is rerun

One quick fix is to put the user input and process pdfs into forms, so that they don’t rerun unless you hit the “submit” button

import streamlit as st
from datetime import datetime
import time


def handle_userinput(user_question):
    st.write(f"Your question: {user_question}, {datetime.now()}")


def process_pdfs(pdf_docs):
    for pdf_doc in pdf_docs:
        st.write(pdf_doc.name)
        time.sleep(1)


st.header("AML Policies and Procedures  :books:")
with st.form("Question"):
    user_question = st.text_input("Ask a question about your documents:")
    if st.form_submit_button("Ask"):
        handle_userinput(user_question)

with st.sidebar:
    st.subheader("Your documents")
    with st.form("Documents"):
        pdf_docs = st.file_uploader(
            "Upload your PDFs here and click on 'Process'", accept_multiple_files=True
        )
        if st.form_submit_button("Process"):
            with st.spinner("Processing"):
                process_pdfs(pdf_docs)
1 Like