【发布时间】:2019-12-10 02:38:29
【问题描述】:
我想在使用widgets.FileUpload 上传文件后创建一个事件,就像我使用.on_click(some_function) 对widgets.Button 所做的一样。有没有办法做这样的事情
import ipywidgets as widgets
def do_something_with_file(fu):
content = fu.data[0].decode('utf-8')
# doing something with the file content and get some string output
return 'some string'
str_file_upload = widgets.FileUpload(accept='.txt', multiple=False)
str_file_upload.on_upload(do_something_with_file)
【问题讨论】:
-
在使用小部件一段时间后,我发现(对我来说)最简单的方法是子类化小部件类并通过相关的 tartlet 监听更改。在这种情况下,这意味着创建一个类
ActionFileUpload(widgets.FileUpload)并添加一个用@observe(''_counter''). This new class will run whatever method appears under@observe(''_counter'') 包装的方法。
标签: python jupyter-notebook widget ipython jupyter