Dropdown values from upload field

I have a upload filed which should trigger the callback to populate the options of a dropdown with values from a row of an uploaded xlsx/csv.

Unfortunately the callback does not get called. When I change the output of the callback from options of the dropdown to the children of some div the callback gets called.

I have seen an example where the uploaded file first gets rendered into a table and then a callback takes the values from the table to populate the dropdown, but this will not work for me as the browser freezes trying to render a table because my files are so large.

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)

I tried your code and indeed, the callbacks don’t fire when you upload a file. I played around a bit and manage to finally trigger callbacks after an upload. What I did was adding a return value to update_col_helper when list_of_contents is None

Try adding ‘return “test”’ when list_of_contents is None and you should see your callbacks fire

Oh, did not come to me that this would be the problem, thank you!