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

Posted

技术标签:

【中文标题】是否可以在 Dash 中上传 csv 文件并将其存储为 pandas DataFrame?【英文标题】:Is it possible to upload a csv file in Dash and also store it as a pandas DataFrame? 【发布时间】:2021-09-11 19:47:09 【问题描述】:

我正在使用 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】:

定义parse_contents函数时,可以简单的return df

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')))
        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 df   

然后,您可以在以下回调中调用parse_contents 并生成熊猫数据框:

@app.callback(
    Output('table-container', 'data'),
    [Input('file_upload', 'contents')],
    State('file_upload', 'filename'))
def filter_df(content, name):
    if content is not None:
    # Return all the rows on initial load/no country selected.
        df = parse_contents(content, name)
        dff = df.to_json()
        dff_pandas = pd.Dataframe(dff)

    else:
        df = parse_contents(content, name)
        dff = df.to_json()
        dff_pandas = pd.Dataframe(dff)
        dff_pandas_filtered = dff_pandas.query('column_A == 012345')

【讨论】:

这个解决方案似乎不起作用。它返回以下错误:Property "data" was used with component ID: "table-container" in one of the Output items of a callback. This ID is assigned to a dash_html_components.Div component in the layout, which does not support this property. This ID was used in the callback(s) for Output(s): table-container.data【参考方案2】:

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

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

【讨论】:

以上是关于是否可以在 Dash 中上传 csv 文件并将其存储为 pandas DataFrame?的主要内容,如果未能解决你的问题,请参考以下文章

在 Dash 中下载 csv 文件

Python 3:如何在不保存在磁盘上的情况下将 pandas 数据帧作为 csv 流上传?

如何在 python(dash)仪表板中显示 png 文件和 csv 表

如何在 firebase 存储上上传多个文件并将访问 url 和元数据作为 json 或 csv 文件获取? [关闭]

Spring Boot 读取用户上传的 csv 文件

php中上传文件的散列内容