上传组件的value值是一个python字典类型;
元数据包括name:文件名称 type:文件格式 size:文件大小 lastModified:最近修改时间
content为文件二进制数据
{
"测试文档.txt": {
"metadata": {
"name": "测试文档.txt",
"type": "text/plain",
"size": 12,
"lastModified": 1647507456106
},
"content": "b'\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe6\xa1\xa3",
}
}
查看数据结构
upload = widgets.FileUpload(
description='上传',
accept='', # 限定接受的文件类型 e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
multiple=False # 是否接受多文件上传
)
submit_buttom = widgets.Button(
description='提交',
layout=Layout(width='30%', height='50px'),
button_style='danger'
)
def btn_click(sender):
print(upload.value)
submit_buttom.on_click(btn_click)
display(upload,submit_buttom)
将上传文件保存到jupyter当前文件夹下
from ipywidgets import interact,widgets
from ipywidgets import interact
from ipywidgets import Button, Layout
import os
#上传组件
upload = widgets.FileUpload(
description='上传',
accept='', # 限定接受的文件类型 e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
multiple=False # 是否接受多文件上传
)
#按钮组件
submit_buttom = widgets.Button(
description='提交',
layout=Layout(width='30%', height='50px'),
button_style='danger'
)
def btn_click(sender):
#
for i in upload.value.keys():
print(i)
f = open(f'./{i}',"wb")
f.write(upload.value[i]["content"])
f.close()
print("finish.")
submit_buttom.on_click(btn_click)
display(upload,submit_buttom)