Upload cvs or excel file to dash

I am trying to store uploaded file as Pandas DataFrame, Is it possible to upload a csv file in Dash and also store it as a pandas DataFrame?

hi @SaadKhan
yes, you can upload a csv file into a dash app. As far as storing it as a Pandas DataFrame, I don’t think so. Here are the types of stored data allowed:

data (dict | list| number | string | boolean; optional): The stored data for the id.

1 Like

Hi @SaadKhan

There is a great example in this post:

2 Likes

I sorted it but changed the example code a lot, returned a Pandas data frame from CSV file, coz I needed to use that Pandas data frame for Decision Tree Algorithm,
Thank You @adamschroeder

Thanks alot

Hey mate, can you share your code on how you did this? I am sure others would benefit from it too.

1 Like
def parse_contents(contents, filename, date):
    content_type, content_string = contents.split(',')

    # define data frame as global
    global df
    global dict_col
    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.'
        ])
    html.Div([
        dcc.Upload(id='upload-data_da',
                   children=html.Div(['Drag and Drop or ', html.A('Select a Single File')]),
                   style={'width': '100%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px',
                          'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px'},
                   # Do not allow multiple files to upload
                   multiple=False
                   ),
        # The below line is for if we want to show the uploaded file as Data Table
        # html.Div(id='output-data-upload_1', hidden=True),
        dbc.Button('Upload File', id='upload_button_da'),
        html.Br(),
        html.Output(id='file_uploaded_da'),
@callback(
    # Output('output-data-upload', 'children'),
    Output('file_uploaded_da', 'children'),
    Input('upload_button_da', 'n_clicks'),
    State('upload-data_da', 'contents'),
    State('upload-data_da', 'filename'),
    State('upload-data_da', 'last_modified'),
    prevent_initial_call=True)
def upload_dataframe(n_clicks, content, filename, date):
    # print(type(df))#this will show data type as a pandas dataframe
    # print(df)

    if filename is not None:
        children = parse_contents(content, filename, date)
        return f'{filename} is Uploaded Successfully...'
        # return children, f"{filename} File Uploaded Successfully"
    else:
        children = parse_contents(content, filename, date)
        return f'No File is Uploaded...'
        # return children, f"No File is Uploaded"
2 Likes

Hi I am new to plotly, can you explain how to implement the above code to get the dataframe?