Is it possible to upload a csv file in Dash and also store it as a pandas DataFrame?

I am developing a dashboard in Dash with Python and in one of the core components I am trying to upload a csv file and display it in a datatable format (see below). That works well (see picture), I followed this example: dcc.Upload | Dash for Python Documentation | Plotly

However, I would also like to use the table as a pandas DataFrame later in the code. Since I upload the csv file after I’ve run the code for the dashboard (deployed on the development server), I could not find a way to return the csv contents as a DataFrame. Any way in which this can be done? My code is below.

NB! I checked the forum for topics similar to this, but I could not find a clear solution.

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

    decoded = base64.b64decode(content_string)
        if 'csv' in filename:
        # Assume that the user uploaded a CSV file
            df = pd.read_csv(
        elif 'xls' in filename:
        # Assume that the user uploaded an excel file
            df = pd.read_excel(io.BytesIO(decoded))
    except Exception as e:
        return html.Div([
            'There was an error processing this file.'
    trade_upload = pd.DataFrame(df)
    return dbc.Table.from_dataframe(trade_upload)

@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(port=8051, debug=False)

Hi @StefaaanP

To save a DataFrame as csv just use:


Hi @Eduardo

Thanks for your reply! That I can do, but I do not want to save it as a csv. I am uploading a csv file, which is then parsed into a table and displayed. I want the uploaded csv to be returned as a dataframe, which I can then use in the code for other calculations within the dashboard.

Hey @StefaaanP

I’m not sure if I understand your problem correctlly. :thinking:

I use to save any dataframe as csv (as explained above) and then read again and use the information in a new DataFrame with:


The information goes from the DataFrame to a csv file and then from the saved csv file into a DataFrame.

Hi @Eduardo

Ah all right, I think I understand what you mean. I will try that!

Thanks again!