使用 Dash 上传和创建 table
Uploading and creating a table using Dash
我是 Plotly Dash 的新手,我想上传一个 csv 文件并创建一个 table。我可以上传 csv 文件,但它是作为元组上传的,而不是作为 pandas DataFrame 上传的,所以我无法使用 TableData 查看它。
app.layout =html.Div([
dcc.Upload(
id='upload',
children=html.Button('Import un fichier'),
),
html.Div(id="output_data"),])
def parse_data(contents, filename):
content_type, content_string = contents.split(",")
decoded = base64.b64decode(content_string)
try:
if "csv" in filename: #csv file
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=',', decimal=','),
print(type(df)),
elif "xls" in filename: #xls file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div(["There was an error processing this file."])
print(df)
return df
@app.callback(
Output("output_data", "children"),
Input("upload", "contents"),
Input("upload", "filename"),
)
def update_table(contents, filename):
# table = html.Div()
if contents:
df=parse_data(contents,filename)
table = html.Div(
[
html.H4(filename),
dash_table.DataTable(
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("records"),
),
html.Hr(),
html.Div("Raw Content"),
html.Pre(
contents[0:200] + "...",
style={"whiteSpace": "pre-wrap", "wordBreak": "break-all"},
),
]
)
return table
我有这个错误AttributeError: 'tuple' object has no attribute 'columns'
这是问题所在:
if "csv" in filename: #csv file
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=',', decimal=','),
print(type(df)),
您在这些行的末尾添加了逗号!这使得值元组。只需删除那些逗号,如下所示:
if "csv" in filename: #csv file
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=',', decimal=',')
print(type(df))
我是 Plotly Dash 的新手,我想上传一个 csv 文件并创建一个 table。我可以上传 csv 文件,但它是作为元组上传的,而不是作为 pandas DataFrame 上传的,所以我无法使用 TableData 查看它。
app.layout =html.Div([
dcc.Upload(
id='upload',
children=html.Button('Import un fichier'),
),
html.Div(id="output_data"),])
def parse_data(contents, filename):
content_type, content_string = contents.split(",")
decoded = base64.b64decode(content_string)
try:
if "csv" in filename: #csv file
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=',', decimal=','),
print(type(df)),
elif "xls" in filename: #xls file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div(["There was an error processing this file."])
print(df)
return df
@app.callback(
Output("output_data", "children"),
Input("upload", "contents"),
Input("upload", "filename"),
)
def update_table(contents, filename):
# table = html.Div()
if contents:
df=parse_data(contents,filename)
table = html.Div(
[
html.H4(filename),
dash_table.DataTable(
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("records"),
),
html.Hr(),
html.Div("Raw Content"),
html.Pre(
contents[0:200] + "...",
style={"whiteSpace": "pre-wrap", "wordBreak": "break-all"},
),
]
)
return table
我有这个错误AttributeError: 'tuple' object has no attribute 'columns'
这是问题所在:
if "csv" in filename: #csv file
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=',', decimal=','),
print(type(df)),
您在这些行的末尾添加了逗号!这使得值元组。只需删除那些逗号,如下所示:
if "csv" in filename: #csv file
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=',', decimal=',')
print(type(df))