Hello there,
I am having a problem trying to dynamically reset a FileInput widget.
More precisely, I have two widgets :
- A MultiSelect with several options
- A FileInput to upload an Excel file enabling to have other selection possibilities that are not in the MultiSelect
I would need the FileInput file to be reset as it is on initialisation with a “No File Chosen” message each time something is selected in the MultiSelect.
Questions :
- How do you catch events on a FileInput object using param variable ? (using param.depends and a variable param.FileSelector)
- How do you modify the value of the FileInput widget so that both the stored value AND the displayed value are reset to initial empty value ?
Here’s a code snippet to illustrate the problem, nothing is printed when an excel file is uploaded :
import param, panel as pn
pn.extension()
#Modify to match any column name of your excel file
COLUMN_NAME = "store"
class Example(param.Parameterized):
trigger_multi_list = param.Number(default=1)
trigger_file_input = param.Number(default=1)
multi_list = param.Parameter(pn.widgets.MultiSelect(options=['Apple', 'Banana', 'Orange', 'Lemon']))
file_input = param.Parameter(pn.widgets.FileInput(accept='.xlsx', name='file_input'))
stores_list = []
@param.depends("multi_list.value", watch=True)
def function_trigger_multi_list(self):
self.trigger_multi_list += 1 # To trigger plot_data_trigger_multi_list
@param.depends("file_input.value", watch=True)
def function_trigger_file_input(self):
with pd.ExcelFile(self.selector.store_file, engine="openpyxl") as excel:
df_excel = pd.read_excel(excel)
self.stores_list = list(df_excel[COLUMN_NAME].dropna().astype("str"))
self.trigger_file_input += 1 # To trigger plot_data_trigger_file_input
@param.depends("trigger_multi_list")
def plot_data_trigger_multi_list(self):
#print(self.multi_list.value, type(self.multi_list.value))
return 'MULTI SELECT: ', self.multi_list.value, self.trigger_multi_list
@param.depends("trigger_file_input", on_init=False)
def plot_data_trigger_file_input(self):
#print(self.stores_list, type(self.stores_list))
return 'FILE INPUT: ', self.stores_list, self.trigger_file_input
def __panel__(self):
return pn.Row(self.multi_list, self.file_input, self.plot_data_trigger_multi_list, self.plot_data_trigger_file_input)
pn.panel(Example().__panel__).servable()