Dash使用加载了上载字段的xlsx / csv中的行填充dcc.Dropdown

时间:2018-11-21 14:57:10

标签: python callback plotly-dash

我的仪表板应用程序中有一个文件输入,该文件输入绑定到应填充下拉列表的回调。

不起作用,我以某种方式忽略了文件上传,当我将回调输出绑定到某个div时,触发了回调。

但这对我没有帮助,因为我想读取数据头作为下拉菜单的选项。

我看到了一个示例,他们使用表作为缓冲区,但是我的数据量太大,无法在表中呈现,所以这对我来说不是一个选择。

dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        multiple=True),

dcc.Dropdown(
    id="select_column",
    options=[{}],
    searchable=True),

在这里回调

@app.callback(Output('select_column', 'options'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename'),
               State('upload-data', 'last_modified'),
               State('separator', 'value')])
def update_col_helper(list_of_contents, list_of_names, list_of_dates, separator):
    if list_of_contents is not None:
        #print('list_of_contents: '+str(list_of_contents))
        print('size of contents: '+str(len(list_of_contents[0])))
        print('list_of_names: '+str(list_of_names))
        print('list_of_dates: '+str(list_of_dates))
        print('seperator: '+separator)
        options = [
            parse_contents(c, n, d, s) for c, n, d, s in
            zip(list_of_contents, list_of_names, list_of_dates, separator)]
        print("options variable: "+str(options))
        return options

完整示例:

import base64
import datetime
import io

from dash.dependencies import Input, Output, State

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt


import pandas as pd
from builtins import str


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

df = None
cols = None


app.layout = html.Div(children=[
    html.H1(children='AK2Viz'),

    html.Div(children='''
        Large Data Visualizer
    '''),

    dcc.RadioItems(
        id='separator',
    options=[
        {'label': 'Semicolon', 'value': ';'},
        {'label': 'Comma or Excel', 'value': ','},
    ],
    value=',',
    labelStyle={'display': 'inline-block'}),

    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '50%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        multiple=True),

    dcc.Dropdown(
        id="select_column",
        options=[{}
             #({}) if ( df is None ) else
             #( {'label': df.iloc[[0][i]], 'value': i} for i in df.iloc[[0]] )
        ],
        searchable=True),

     html.Div(id='output-container',
              children="helper div"),
     html.Div(id='col_helper', children='col_helper')
])


@app.callback(
    dash.dependencies.Output('output-container', 'children'),
    [dash.dependencies.Input('select_column', 'value')])
def update_output(value):
    return 'You have selected "{}"'.format(value)

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

    decoded = base64.b64decode(content_string)
    print('decoded len: '+str(len(decoded)))
    print('decoded raw: '+str(decoded[0:100]))
    print('io.Bytes len: '+str(io.BytesIO(decoded).getbuffer().nbytes))
    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), sheet_name=1)
            print('df.empty: '+str(df.empty))
            print('row for drodown: '+str(df.iloc[[0]]))
            print('len of row for drodown: '+str(len(df.iloc[[0]])))
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])
    return [{'label': i, 'value': i } for i in df.iloc[[0]]]



@app.callback(Output('select_column', 'options'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename'),
               State('upload-data', 'last_modified'),
               State('separator', 'value')])
def update_col_helper(list_of_contents, list_of_names, list_of_dates, separator):
    if list_of_contents is not None:
        #print('list_of_contents: '+str(list_of_contents))
        print('size of contents: '+str(len(list_of_contents[0])))
        print('list_of_names: '+str(list_of_names))
        print('list_of_dates: '+str(list_of_dates))
        print('seperator: '+separator)
        options = [
            parse_contents(c, n, d, s) for c, n, d, s in
            zip(list_of_contents, list_of_names, list_of_dates, separator)]
        print("options variable: "+str(options))
        return options

if __name__ == '__main__':
    app.run_server(debug=True)

0 个答案:

没有答案