ipywidgets
ipywidgets copied to clipboard
Reset FileUpload widget?
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?
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?
closing and recreating the widget worked for me, thank you.
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.
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?
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 = []
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:

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!
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! :)