Reset FileInput with a watcher on its value

Hi @hadmaria,

For the questions, one way I’ve seen is param.watch see in code below to capture an event. Maybe try integrating something like this into your code, the idea I think here is that a widget to restore to default that your looking for I think you need to add to a pn.Row or pn.Column so that it can be dynamically removed and added back. I don’t think you can simply change the value, perhaps you can I’ve not explored. This seems to be what your looking for with regards to resetting FileSelector, one caveat here is that you can’t re click on the already selected option in multi widget, you must click another

import panel as pn

class WidgetManager:
    def __init__(self):
        self.load_row = self.load_file_widget()
        self.widget_row = self.load_widgets()

    def load_file_widget(self):
        # Create a row of widgets for file load
        self.file_input = pn.widgets.FileInput(accept='.csv')
        load_row = pn.Row(self.file_input)
        return load_row

    def load_widgets(self):
        # Create a row of widgets
        self.multi_select = pn.widgets.MultiSelect(name='MultiSelect', options=['Option 1', 'Option 2', 'Option 3'])
        widget_row = pn.Row(self.multi_select)
        # Add callback function to MultiSelect widget
        self.multi_select.param.watch(self.reset_file_input, 'value')
        return widget_row

    def reset_file_input(self, event):
        print("reset file input")
        # Reset FileInput widget when MultiSelect widget is clicked
        # first remove, initially tried pop but clear seems to be the ticket here
        self.load_row.clear()
        # now you need to recreate the widget
        self.load_row.append(pn.widgets.FileInput(accept='json', multiple= False))


# Create an instance of the WidgetManager class
widget_manager = WidgetManager()



# Create a FastList dashboard and add the widgets to it
dashboard = pn.Column(pn.Column(
    widget_manager.widget_row,
    sizing_mode='stretch_width'),
                      pn.Column(widget_manager.load_row,)
)

# Show the dashboard
dashboard.show()

Another way not using class but quite similar whom I got the idea from is @riziles see the contribution here

https://discourse.holoviz.org/t/clear-fileinput-widget-after-file-uplaod/4759

Hope of some help, Carl.

2 Likes