python - 将 CSV 上传到 Plotly Dash 并基于 Pandas 数据框渲染条形图

标签 python pandas plotly plotly-dash

我是新来的 Plotly Dash框架并尝试构建一个简单的仪表板:

  • 允许用户上传 CSV 文件以进行图形分析。
  • 根据步骤 1 中上传的文件创建 Pandas 数据框。

    2a.如果未选择 CSV 文件(和结果数据框),则不呈现任何内容。
  • 根据上述数据框中包含的数据呈现基本条形图(或散点图、热图等)。

  • 我的 CSV 文件中的数据类似于以下内容:
    df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'BMW', 'BMW', 'BMW', Mercedes', 'Mercedes', 'Mercedes'],
                              'Score':['88.6', '76.6', '100', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
                              'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
                              'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
    

    我的代码如下:
    import base64
    import datetime
    import io
    import dash
    from dash.dependencies import Input, Output, State
    import dash_core_components as dcc
    import dash_html_components as html
    import plotly.graph_objects as go
    import dash_table
    import pandas as pd
    
    
    app = dash.Dash()
    
    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'
             },
            # Allow multiple files to be uploaded
            multiple=True
    ),
    
    html.Div(id='output-data-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.'
            ])
    
        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
    
            #### How to get the x and y values DYNAMICALLY from the data frame to pass into the Bar() function? ####
    
        dcc.Graph(
            figure = go.Figure(data=[
            go.Bar(name=df.columns.values[0], x=pd.unique(df['Make']), y=[88.6, 76.6, 100], text=[88.6, 76.6, 100], textposition='auto'),
            go.Bar(name=df.columns.values[1], x=pd.unique(df['Make']), y=[92.5, 93.6, 93.4], text=[92.5, 93.6, 93.4], textposition='auto'),
            go.Bar(name=df.columns.values[2], x=pd.unique(df['Make']), y=[99.1, 99.2, 95.9], text=[99.1, 99.2, 95.9], textposition='auto'),
            ])
            ),        
    
    
            html.Hr(),
    
            # 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'
            })
        ])
    
    @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(debug=True)
    

    我可以上传和查看 CSV 文件的内容。

    然而,go.Bar()函数有它的xy值“硬编码”。它们不一定是动态的(即如果 x 变量的数量发生变化等)。

    如何让 Dash 根据使用 parse_contents(contents, filename, date) 上传的 CSV 文件中的数据构建条形图功能?

    我试图在 Using dash upload component to upload csv file and generate a graph 中跟进,但无法成功实现该示例。

    在此先感谢您帮助新手使这个玩具示例正常工作!

    最佳答案

    这是答案:

    import base64
    import datetime
    import io
    import dash
    from dash.dependencies import Input, Output, State
    import dash_core_components as dcc
    import dash_html_components as html
    import plotly.express as px
    import plotly.graph_objects as go
    import dash_table
    import pandas as pd
    
    
    app = dash.Dash()
    
    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'
             },
            # Allow multiple files to be uploaded
            multiple=True
    ),
    
    html.Div(id='output-data-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.'
            ])
    
        return html.Div([
    
            dcc.Graph(
                figure = go.Figure(data=[
                go.Bar(name=df.columns.values[0], x=pd.unique(df['Make']), y=df['Score'], text=df['Score'], textposition='auto'),
                ])
                ),        
    
    
        ])
    
    @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(debug=True)
    

    关于python - 将 CSV 上传到 Plotly Dash 并基于 Pandas 数据框渲染条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62097062/

    相关文章:

    Python 子进程返回错误的退出代码

    python - Pycrypto 在 Windows 上构建 pycrypto 的轮子失败

    r - R 中子图 plot_ly 中每个图附近的图例

    r - 强制 Plotly 相关热图色标在零处为白色 - R

    python - 是否可以将 Python Seaborn 与 plotly 集成

    Python AWS Lambda 事件 JSON

    python - mac osx雪豹中的pyqt安装问题

    python - DataFrame.stack() 后的新索引级别名称

    python - 类型错误 : unsupported operand type(s) for +: 'Timestamp' and 'float'

    python - 根据值拆分 pandas 数据框