【发布时间】:2020-04-13 13:18:58
【问题描述】:
我想使用 Dash 上传组件(请参阅 Example from Dash Documentation)上传 csv 文件并将其发布到文件服务器。我希望它像working Postman csv Post-Request 一样简单。 我的回调当前如下所示:
@app.callback(
Output(component_id="eventlog-dropdown", component_property="options"),
[Input(component_id="upload-data", component_property="contents"),
Input(component_id="upload-data", component_property="filename")]
)
def upload_eventlog(uploaded_contents, uploaded_filename):
if uploaded_contents is not None:
eventlog_data = [
parse_contents(c, n) for c, n in
zip(uploaded_contents, uploaded_filename)]
files = {'file': (uploaded_filename[0], str(eventlog_data))}
print(files)
requests.post(MIDDLEWARE_ENDPOINT+"data/upload-eventlog", files=files)
#Updates the dropdown "eventlog-dropdown"
files = requests.get(MIDDLEWARE_ENDPOINT + 'data/geteventloglist').json()
options = [{'label': i, 'value': i} for i in files]
return options
elif uploaded_contents is None:
files = requests.get(MIDDLEWARE_ENDPOINT + 'data/geteventloglist').json()
options = [{'label': i, 'value': i} for i in files]
return options
解析内容函数如下所示:
def parse_contents(contents, filename):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
return df
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
return df
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
帖子“有效”。它发布 csv 文件,并且 csv 文件存储在文件服务器上。但是,csv 不再以逗号分隔并被截断:
69 FN322 Landing 2019-05-22 04:35 Berlin 12:20 4 12.333333
70 FN322 Baggage claim 2019-05-22 03:05 Berlin 01:40 5 1.666667
[71 rows x 7 columns]]
你们知道像 Postman 那样在 Dash Upload 组件中发布上传的文件名和内容的简单方法吗?或者我怎样才能至少保存所有文件内容?
非常感谢您,祝您有美好的一天!
【问题讨论】:
标签: csv post python-requests postman plotly-dash