如何处理 Dash 中上传的 zip 文件?
How to handle uploaded zip file in Dash plotly?
使用 dcc.Upload
,您可以在 Dash Plotly 仪表板中构建拖放或基于按钮的上传功能。但是,文档中对处理特定文件类型(如 .zip
)有限制。这是上传的片段 html:
dcc.Upload(
id='upload_prediction',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files'),
' New Predictions (*.zip)'
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
accept=".zip",
multiple=True
)
然后当我尝试使用此代码段检查上传的文件时:
@app.callback(Output('output_uploaded', 'children'),
[Input('upload_prediction', 'contents')],
[State('upload_prediction', 'filename'),
State('upload_prediction', 'last_modified')])
def test_callback(list_of_contents, list_of_names, list_of_dates):
for content in list_of_contents:
print(content)
上传后的content-type为‘data:application/x-zip-compressed;base64’。如何在 Dash Plotly 中处理此类文件(例如将其提取到某处)?
在 plotly 论坛上问了一个类似的问题,但没有答案:
https://community.plot.ly/t/dcc-upload-zip-file/33976
Dash Plotly提供base64字符串格式的上传文件。您需要做的是先对其进行解码,然后将其作为字节字符串处理,稍后可用于初始化ZipFile
class(这是python中的内置工具)。
import io
import base64
from zipfile import ZipFile
@app.callback(Output('output_uploaded', 'children'),
[Input('upload_prediction', 'contents')],
[State('upload_prediction', 'filename'),
State('upload_prediction', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
for content, name, date in zip(list_of_contents, list_of_names, list_of_dates):
# the content needs to be split. It contains the type and the real content
content_type, content_string = content.split(',')
# Decode the base64 string
content_decoded = base64.b64decode(content_string)
# Use BytesIO to handle the decoded content
zip_str = io.BytesIO(content_decoded)
# Now you can use ZipFile to take the BytesIO output
zip_obj = ZipFile(zip_str, 'r')
# you can do what you wanna do with the zip object here
使用 dcc.Upload
,您可以在 Dash Plotly 仪表板中构建拖放或基于按钮的上传功能。但是,文档中对处理特定文件类型(如 .zip
)有限制。这是上传的片段 html:
dcc.Upload(
id='upload_prediction',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files'),
' New Predictions (*.zip)'
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
accept=".zip",
multiple=True
)
然后当我尝试使用此代码段检查上传的文件时:
@app.callback(Output('output_uploaded', 'children'),
[Input('upload_prediction', 'contents')],
[State('upload_prediction', 'filename'),
State('upload_prediction', 'last_modified')])
def test_callback(list_of_contents, list_of_names, list_of_dates):
for content in list_of_contents:
print(content)
上传后的content-type为‘data:application/x-zip-compressed;base64’。如何在 Dash Plotly 中处理此类文件(例如将其提取到某处)?
在 plotly 论坛上问了一个类似的问题,但没有答案: https://community.plot.ly/t/dcc-upload-zip-file/33976
Dash Plotly提供base64字符串格式的上传文件。您需要做的是先对其进行解码,然后将其作为字节字符串处理,稍后可用于初始化ZipFile
class(这是python中的内置工具)。
import io
import base64
from zipfile import ZipFile
@app.callback(Output('output_uploaded', 'children'),
[Input('upload_prediction', 'contents')],
[State('upload_prediction', 'filename'),
State('upload_prediction', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
for content, name, date in zip(list_of_contents, list_of_names, list_of_dates):
# the content needs to be split. It contains the type and the real content
content_type, content_string = content.split(',')
# Decode the base64 string
content_decoded = base64.b64decode(content_string)
# Use BytesIO to handle the decoded content
zip_str = io.BytesIO(content_decoded)
# Now you can use ZipFile to take the BytesIO output
zip_obj = ZipFile(zip_str, 'r')
# you can do what you wanna do with the zip object here