I can't seem to figure out the syntax for triggering a function upon someone using a FileInput widget in a Parameterized class.
I understand that FileInput isn't a param itself, but I looked at the code for it and the value attribute is a generic param.Parameter, so I thought this would work. I also tried just depending on file (@param.depends('file')
).
class MyFile(param.Parameterized):
file = pn.widgets.FileInput() # should be a param.?
file_data = None
@param.depends('file.value')
def process_file(self):
print('processing file')
self.file_data = self.file.value
my_file = MyFile()
Then after using the file widget, I would expect my_file.file_data
to have the same contents of self.file.value
.
panel_output
Appreciate any input or if anyone can point me to appropriate docs. Thanks!
https://github.com/pyviz/panel/issues/711
You are right, in this case your 'file' variable needs to be a param, not a panel widget.
All possible options there are for setting available params are here:
https://param.pyviz.org/Reference_Manual/param.html
So in your case I used param.FileSelector():
import param
import panel as pn
pn.extension()
class MyFile(param.Parameterized):
file = param.FileSelector() # changed this into a param
file_data = None
@param.depends('file', watch=True) # removed .value and added watch=True
def process_file(self):
print('processing file')
self.file_data = self.file # removed .value since this is a param so it's not needed
my_file = MyFile()
This FileSelector is however a box to type the filename yourself. This question is related to this and gives some more explanation:
Get a different (non default) widget when using param in parameterized class (holoviz param panel)
So you need to change this FileSelector still to the FileInput widget, by overwriting it like this:
pn.Param(
my_file.param['file'],
widgets={'file': pn.widgets.FileInput}
)
Please note that I also added watch=True. This makes sure that changes get picked up, when your 'file' param has changes. There's a bit more explanation of this in the following question:
How do i automatically update a dropdown selection widget when another selection widget is changed? (Python panel pyviz)
Can you let me know if this helped?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With