ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

Reset FileUpload widget?

Open fimafurman opened this issue 5 years ago • 8 comments

I tried to find examples on how to reset widgets to default state. Specifically, I need to reset the FileUpload widget after upload so that it defaults to an empty widget. Is this possible, or is this a feature request?

fimafurman avatar Dec 30 '19 21:12 fimafurman

Maybe by taking a snapshot of the previous state with get_state, and applying it back to the widget later on?

Otherwise recreating the widget might also be a viable solution?

jtpio avatar Jan 06 '20 16:01 jtpio

closing and recreating the widget worked for me, thank you.

fimafurman avatar Jan 08 '20 17:01 fimafurman

Closing and recreating the FileUpload widget lead to my widget disappeared, my FileUpload widget is wrapped in ipywidgets.VBox. However, simply recreating the object to the same variable does no effect. Any update in this issue report? To your reference, I also tried set value of FileUpload widget to empty dict {} by using dict.clear(). The file number in the icon is not changed to zero. ~And I am sure the value is still store in the widget object.~

EDIT: Ha! find setting upload._counter = 0 working for me. To recap reset FileUpload widget by running following code(event when the widget wrapped in VBox):

import ipywidgets as ipw
uploader = ipw.FileUpload()
vb = ipw.VBox([uploader])
display(vb)

# a upload box show then click it and upload the test file
upload.value.clear()
upload._counter = 0

# then you can find the widgets get updated.  

unkcpz avatar Aug 06 '20 09:08 unkcpz

With the changes to FileUpload widget in ipywidgets 8.0.0 this does not work anymore. Any ideas how to achieve the same in ipywidgets 8.0.0?

ffeldhaus avatar Dec 20 '21 12:12 ffeldhaus

It seems you now have to set value to an empty array. E.g.

import ipywidgets as ipw
# a upload box show then click it and upload the test file
uploader = ipw.FileUpload()
display(uploader)

# to reset the counter to 0 use
uploader.value = []

ffeldhaus avatar Dec 20 '21 16:12 ffeldhaus

I couldn't use this method, error pops up: TraitError: The "value" trait is read-only.. I am using ipywidgets=7.7.1 and python=3.9.13.

Instead, I use:

uploader = [] 

Ref: image

yuenherny avatar Jun 29 '22 12:06 yuenherny

I think resetting the file upload widget is an excellent feature request. Right now, I think the best way to reset the upload widget is to create a new one and replace it in your UX. However, I'm also intrigued by the various workarounds people have posted above. Thanks!

jasongrout avatar Jun 30 '22 06:06 jasongrout

Note: The FileUpload widget has had a major (backwards incompatible) overhaul in 8.0. We'd love if someone could test how the experience with the new implementation would be w.r.t this issue. If we need to make any changes to it, the RC window is closing fast! :)

vidartf avatar Jul 15 '22 09:07 vidartf