Reset ipywidget Fileupload button after upload of the files

Viewed 352

I am trying to have the Upload(N) button of the FileUpload widget from Ipywidgets to reset the number N after i uploaded some files, but I'm not sure how. I am using Jupiter notebook with:

uploader=widgets.FileUpload(
    accept='*.csv',  # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
    multiple=True  # True to accept multiple files upload else False
)

def update_list_files(*args):
    global glob_list_f            #if i make the variable global then i can take it out of the function.
    glob_list_f = list(uploader.value.keys()) 

    
uploader.observe(update_list_files, 'value')
uploader

If I select 3 files it yields : Upload(3)

Now If I select 3 other files it yields: Upload(6)

The wanted behaviour would be Upload(3) again.

1 Answers

In my case, I need to upload file again. I use next logic to reset button file upload state:

Click handling Function:

def button_click_func(change):
 # ... business logic

  upload_alg.value.clear()  # <--- clear saved value in cache
  upload_alg._counter=0     # <--- reset counter to visualize 

Connect handler:

upload_file_button.observe(button_click_func, 'value')
Related