Dash应用程序错误:没有足够的值可解压缩(预期2,得到1)

时间:2019-06-24 19:10:08

标签: python pandas plotly plotly-dash

我正在尝试通过查看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,boilerData.csv中的csv文件测试此文件

1 个答案:

答案 0 :(得分:0)

Parse_contents使用分隔符,默认情况下为逗号 拆分内容本身时,不能将其分配给2个变量,因为只能分配一个值。使用content_string = parse_contents(x,y)

重试