Summary
I have a form with file uploader with option to upload multiple files. When I click submit with callback method. The first time, the callback method passes empty file list and only second time is the file list passed
Steps to reproduce
Code snippet:
def save_files(files):
print('inside save_files: ', files)
doc_dir = 'documents'
if len(files) > 0:
for file in files:
with open(os.path.join(doc_dir, file.name), 'wb') as f:
f.write(file.getbuffer())
st.session_state.uploaded_files.append({'Name': file.name,
'Type': file.type,
'Size': str(file.size),
'Summary': 'TBC'})
st.session_state.disabled = True
return True
else:
st.session_state.disabled = False
return False
def main():
col1, col2 = st.columns(2)
st.sidebar.title('File Upload and Processing')
with st.sidebar.form(key='sidebar_form'):
# Allow the user to upload a files
uploaded_files = st.file_uploader('Upload files',
type=['pdf', 'txt'],
key='file_upload_widget',
accept_multiple_files=True,
disabled=st.session_state.disabled)
# If a files was uploaded, display its contents
submit_btn = st.form_submit_button('Upload Files',
on_click=save_files,
args=(uploaded_files,),
disabled=st.session_state.disabled)
if submit_btn:
st.sidebar.write('No more upload possible')
As you can see save_files has print statment
on the terminal I see
On first Submit click
inside save_files:
On second time submit click
inside save_files: [UploadedFile(id=1, name=â22080200056474.pdfâ, type=âapplication/pdfâ, size=81932)]
Expected behavior:
On first Submit click, I should see on terminal
inside save_files: [UploadedFile(id=1, name=â22080200056474.pdfâ, type=âapplication/pdfâ, size=81932)]
Actual behavior:
I would expect that first Submit should pass the list of files uploaded (in this example I tried to upload only file but problem exist for multiple files also