St.file_uploader on_change callback

Hi there, I am new to Streamlit and having issues with the st.file_uploader callback. I have a Semantic search app which has an input box for the url and another input box for the search question. The user also has an option to upload a file and search using the search input box. TO provide a good user experience, I was trying to clear both input boxes once the user uploads a file so that they can type in the search question but it keeps continuously clearing the text in the Search query. It’s like the callback keeps running persistently.

if "text_url" not in st.session_state:
  st.session_state["text_url"] = "https://www.rba.gov.au/monetary-policy/rba-board-minutes/2022/2022-05-03.html"
  
if "text_input" not in st.session_state:
  st.session_state["text_input"] = "What are the expectations for inflation for Australia?"


def clear_text():
    st.session_state["text_url"] = " "
    st.session_state["text_input"]= " "
    
def clear_search_text():
    st.session_state["text_input"]= " "
    
url_text = st.text_input("Please Enter a url here",key='text_url',on_change=clear_search_text)

st.markdown(
    "<h3 style='text-align: center; color: red;'>OR</h3>",
    unsafe_allow_html=True,
)

upload_doc = st.file_uploader(
    "Upload a .txt, .pdf, .docx file"
,on_change=clear_text, key="doc_uploaded")

search_query = st.text_input("Please Enter your search query here",key="text_input")

if validators.url(url_text):
    #if input is URL
    title, text = extract_text_from_url(url_text)
    passages = preprocess_plain_text(text,window_size=window_size)
    
elif upload_doc:
   
    text, pdf_title = extract_text_from_file(upload_doc)
    passages = preprocess_plain_text(text,window_size=window_size)```

@snehankekre this was my specific issue I was having, not sure if it is related to the bug. Seems like it might be, would you be kind to help with a workaround?

And this issue is still present. Its a critical bug considering how efficient on_change functionality is to the widget uses.

1 Like

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