File_uploader triggering second time when we click on other buttons in the same page

Hi,
I am trying to create question and answering system for uploaded file. I use side bar for uploading files, and calling one of my webserivice to store in the db. after that i created a text field for question entering followed by a ask button. but whenever we click ask button after file uploaded its again uploading the file. even I am calling two different webservices.

st.sidebar.header("upload files to perform actions")
file_object= st.sidebar.file_uploader('upload text files',type=['txt','pdf'])

if file_object is not  None:
   print(file_object.name)
   print(file_object.type)
   with st.spinner("Please wait while file uploading into server" ):
       files = {"file": (file_object.name, file_object, "multipart/form-data")}

       status=requests.post("http://127.0.0.1:8000/readfileandstore/",files=files).json()
       logging.info("status code")
       logging.info(status)

   st.sidebar.info("successfully uploaded ")

question = st.text_input("Please provide your query:",)
runquery= st.button('Ask')

if runquery:
    if question:
        with st.spinner("Performing neural search on documents",):
            headers={"accept": "application/json" ,"Content-Type": "application/json"}

            data = {"question": f'"{question}"'}
            results=requests.post(' http://127.0.0.1:8000/findanswer',headers=headers,
                                  json=data).json()

        logging.info(results

I came to know i need to use state. but cannot move forward. any help is highly appreictaed.

Hi @NSVR ,

Welcome ! Glad to come across you in the Streamlit Community.

Probably this happens, because the app reruns from the very beggining. One possible way to avoid this can be , by wrapping your very first few lines of code within a function and use @st.cache before it. In that way, the function only reruns if it sees there’s a new argument . Do refer to the doc as well.
Here’s a snippet below from your tried piece of codes, this can be helpful,

import streamlit as st

@st.cache(suppress_st_warning=True)
def fup(file_object):  
    if file_object is not  None:
        st.sidebar.success("successfully uploaded!")
        bytes_data = file_object.getvalue()
        st.info("The fup function is executed!")
        
    return bytes_data

st.sidebar.header("upload files to perform actions")

file_object= st.sidebar.file_uploader('upload text files',type=['txt','pdf'])
if file_object is not  None:    
    btn = st.sidebar.button("Show data!")
    if btn :
        data = fup(file_object)
        st.write(data)

if you remove the @st.cache(suppress_st_warning=True) and try the code, you will find the that the function runs everytime you press the Show data button! This you can monitor because of the st.info("The fup function is executed!") pops up ! Also refer to the doc .

Anothe way is to store your files data using st.st.session_state , for example in above case : st.session_state.value = data (don’t forget to initialize session state before hand, or an error will pop up), refer to the doc as well

Cheers
Avra

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