Clear fileinput widget after file uplaod

I have a working fileinput widget.

upload =  pn.widgets.FileInput(accept='json', multiple= False)
upload.param.watch(process_data, 'value')

My dashboard updates with process_data(). However if the user wishes to upload a second file, with the same name (which is the majority use case), the watcher will not trigger.

I believe this can be attributed to the tag not clearing, as one might do using javascript with an html form after the form has been submitted.

I’ve tried the obvious…

upload.value = {}

Is there a way to clear the form after upload so that the watcher recognises the file a new upload?

Festive Greetings.

Holy smokes! This is a lot harder than I thought. Would love to hear from one of the experts like @Hoxbro , @maximlt or @Marc . The only way I could figure out how to do it was to completely pop and replace the widget:

upload =  pn.Row(
    pn.indicators.Number(value = 0),
    pn.widgets.FileInput(accept='json', multiple= False, name = 'x')
)

def process_data(*data, upload = upload):
    for datum in data:
        if hasattr(datum, 'name') and datum.name == 'value' and isinstance(datum.new, bytes):
            
            print('x')
            fmr = upload.pop(1)
            fmr.value = None
            upload.append(pn.widgets.FileInput(accept='json', multiple= False))
            upload[0].value = upload[0].value + 1
            upload[1].param.watch(process_data, ['value'])

            
upload[1].param.watch(process_data, 'value')

upload

Apparently, I’m an idiot and accidentally solved something I couldn’t figure out here in another post?

1 Like