[英]How to update Dashboard (dash/plotly) with new data uploaded to the folder?
[英]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)
上傳后的內容類型是'data:application/x-zip-compressed;base64'。 如何在 Dash Plotly 中處理此類文件(例如將其提取到某處)?
在 plotly 論壇中問了一個類似的問題,沒有答案: https ://community.plot.ly/t/dcc-upload-zip-file/33976
Dash Plotly 以 base64 字符串格式提供上傳的文件。 您需要做的是首先對其進行解碼,然后將其作為字節字符串處理,稍后可用於初始化ZipFile
類(它是 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.