python - 使用 ipywidgets FileUpload 小部件时,我可以定义文件上传操作吗
问题描述
我想在上传文件时创建一个事件,widgets.FileUpload
就像我widgets.Button
使用.on_click(some_function)
. 有没有办法做这样的事情
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)
解决方案
可以观察 _counter 的变化
例子:
from ipywidgets import widgets
uploader = widgets.FileUpload()
display(uploader)
def on_upload_change(change):
print(change)
uploader.observe(on_upload_change, names='_counter')
推荐阅读
- javascript - 不和谐重复机器人
- javascript - 是否可以使用 Jquery 保留 img 标签和仅其他标签的文本?
- symfony - 如何仅显示特定行的数据
- python - 如何检测pygame中嵌套按钮内的鼠标点击
- python - 如何使用 django 建立一对一的聊天系统
- wordpress - 为什么我的 wordpress 本地化 var 在我的前端 js 中不可用
- ios - 使用 SwiftUI 在多个 iOS 设备上显示具有精确尺寸的标尺图像
- amazon-web-services - 为 AWS Elastic Beanstalk 环境使用 namecheap 域名?
- json - 替换其他键所在的值 - jq
- json - 将列表添加到来自 JSON 数据的 Picker.Item 组件反应本机博览会