mig*_*drs 6 python parameters pyviz panel-pyviz
我似乎无法弄清楚在参数化类中使用 FileInput 小部件的人触发函数的语法。
我知道 FileInput 本身不是一个参数,但我查看了它的代码,并且 value 属性是一个通用的 param.Parameter,所以我认为这会起作用。我也尝试过仅依赖于文件 ( @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()
Run Code Online (Sandbox Code Playgroud)
然后,使用文件控件后,我希望my_file.file_data有相同内容self.file.value。
感谢任何输入,或者是否有人可以将我指向适当的文档。谢谢!
您是对的,在这种情况下,您的 'file' 变量需要是 param,而不是面板小部件。
用于设置可用参数的所有可能选项都在这里:https :
//param.pyviz.org/Reference_Manual/param.html
所以在你的情况下,我使用了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()
Run Code Online (Sandbox Code Playgroud)
然而,这个 FileSelector 是一个你自己输入文件名的框。这个问题与此相关并给出了更多解释:
Get a different (non default) widget when using param in parameterized class (holoviz param panel)
因此,您需要将此 FileSelector 更改为 FileInput 小部件,方法是像这样覆盖它:
pn.Param(
my_file.param['file'],
widgets={'file': pn.widgets.FileInput}
)
Run Code Online (Sandbox Code Playgroud)
请注意,我还添加了watch=True。当您的“文件”参数发生更改时,这可确保更改被接收。在以下问题中对此有更多解释:
如何在更改另一个选择小部件时自动更新下拉选择小部件?(Python 面板 pyviz)
你能告诉我这是否有帮助吗?