I am writing a script that will update the contents of a variable when a new file uploaded using st.file_uploader(). I am trying to achieve this using the ‘on_change’ function and accessing the new value via the key_field. However, after the first file is successfully uploaded, when I upload a second file, I instead get an error “‘NoneType’ object has no attribute ‘read’”
Below is my code:
def load_new_file():
with st.spinner("Please wait.. loading the file"):
new_file = st.session_state["new_file"]
bytes_data = new_file.read()
upload_file_to_blob(STORAGE_CONNECTION_STRING, STORAGE_CONTAINER_NAME, "app_data/"+new_file.name, bytes_data)
st.session_state.document_url = get_blob_sas_url(STORAGE_CONNECTION_STRING, STORAGE_CONTAINER_NAME, "app_data/"+new_file.name)
return
st.session_state.uploaded_file = st.file_uploader("Choose the PDF file", accept_multiple_files=False, key="new_file", on_change=load_new_file)
I get this error on the line " bytes_data = new_file.read()" everytime I upload a second file and the callback function gets triggered. This doesn’t happen when I upload the file for the first time though. Any idea what could be causing the issue?
I am running the code locally on a Python 3.9 environment with streamlit version 1.28.2