【发布时间】:2019-06-24 19:10:08
【问题描述】:
我正在尝试通过查看uploads tutorial 来使用 Dash 创建一些简单的东西,但执行的是绘制数据图表而不是创建表格。
有人对我在解析数据时遇到的错误有任何提示吗?
File "C:\Users\benb\Desktop\dash\hwsLineUpload.py", line 105, in update_graph
df = parse_contents(contents, filename)
File "C:\Users\benb\Desktop\dash\hwsLineUpload.py", line 52, in parse_contents
content_type, content_string = contents.split(',')
ValueError: not enough values to unpack (expected 2, got 1)
这是完整的脚本,我知道我的 def parse_contents(contents, filename): 函数有问题。任何提示都有帮助,这里没有太多智慧。
import base64
import datetime
import io
import cufflinks as cf
import plotly.graph_objs as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {
"graphBackground": "#F5F5F5",
"background": "#ffffff",
"text": "#000000"
}
app.layout = html.Div([
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Not multiple files to be uploaded
multiple=False
),
dcc.Graph(id='myGraph')
])
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')))
if df.empty:
df = df.fillna(method = 'ffill').fillna(method = 'bfill')
if df.isnull().values.any():
df = df.fillna(method = 'ffill').fillna(method = 'bfill')
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
if df.empty:
df = df.fillna(method = 'ffill').fillna(method = 'bfill')
if df.isnull().values.any():
df = df.fillna(method = 'ffill').fillna(method = 'bfill')
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
return df
@app.callback(Output('myGraph', 'figure'),
[
Input('upload-data', 'contents'),
Input('upload-data', 'filename')
])
def update_graph(contents, filename):
fig = {
'layout': go.Layout(
plot_bgcolor=colors["graphBackground"],
paper_bgcolor=colors["graphBackground"])
}
if contents:
contents = contents[0]
filename = filename[0]
df = parse_contents(contents, filename)
df = df.set_index(df.columns[0])
fig['data'] = df.iplot(asFigure=True, kind='scatter', mode='lines+markers', size=1)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
我正在使用我在this git repo 中的 csv 文件进行测试的文件,boilerData.csv
【问题讨论】:
-
这通常意味着
contents中没有,可以拆分 -
我还尝试将 Excel 中的 CSV 文件保存到 Excel workbook.xls,但我得到了同样的错误...您有什么建议可以尝试吗?
-
这里好像不能使用
return df,需要返回html.Div([ html.H5(filename),]之类的。为了您的目标(我认为,您需要 csv 文件) - 您可以在 this 代码 sn-p 中寻找见解。它可以帮助我在物体识别后下载图片;所以我希望这能帮助你下载你的 csv
标签: python pandas plotly plotly-dash