gpt4 book ai didi

python - 是否可以在 Dash 中上传 csv 文件并将其存储为 pandas DataFrame?

转载 作者:行者123 更新时间:2023-12-05 05:59:24 25 4
gpt4 key购买 nike

我正在使用 Python 在 Dash 中开发一个仪表板,在我试图上传一个 csv 文件并以数据表格式显示它的核心组件之一中(见下文)。这很好用(见图),我按照这个例子:https://dash.plotly.com/dash-core-components/upload

但是,我还想在代码的后面将该表用作 pandas DataFrame。由于我在运行仪表板代码后上传了 csv 文件,因此我找不到将 csv 内容作为 DataFrame 返回的方法。有什么方法可以做到这一点?我的代码如下。

Dash app output

提前致谢!

###############################################################################
# Upload files
# https://dash.plotly.com/dash-core-components/upload
###############################################################################

def parse_contents(contents, filename, date):
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')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])

trade_upload = pd.DataFrame(df)
return dbc.Table.from_dataframe(trade_upload)

@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents')],
[State('upload-data', 'filename'),
State('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children

if __name__ == '__main__':
app.run_server(port=8051, debug=False)

最佳答案

您可以将其保存为全局变量。这是单个文件上传的代码。

1.布局

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'
},
# Allow multiple files to be uploaded
multiple=False
),

html.Div(id='output-data-upload'),


])

2.功能

def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')

global df#define data frame as global
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')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])

return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),

dash_table.DataTable(
data=df.to_dict('records'),
columns=[{'name': i, 'id': i} for i in df.columns]
),

html.Hr(), # horizontal line

# For debugging, display the raw contents provided by the web browser
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])

3.回调

@app.callback(Output('output-data-upload', 'children'),
Input('upload-data', 'contents'),
State('upload-data', 'filename'),
State('upload-data', 'last_modified'))
def update_output(content, filename, date):
children=parse_contents(content, filename, date)
print(type(df))#this will show data type as a pandas dataframe
print(df)
return children

关于python - 是否可以在 Dash 中上传 csv 文件并将其存储为 pandas DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68181416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com