Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

How to load a file path from a callback?

I have the following function:

def origin_func():

return html.Div(                                                                  
    children=[                                                                    
        dcc.Markdown('Select country'),                              
        dcc.Dropdown(                                                             
            id='origin_div',                                                      
            options=[{'label': 'Malta', 'value': './data/malta.csv'}, {'label': 'Japan', 'value': './data/japan.csv'}],
            value='./data/malta.csv'                                   
        )                                                                         
    ]                                                                             
)

That allows associating a country with a file path (html.Div(children=[origin_func()])).

Now, I’d would like to use this path to load a Pandas Dataframe through a callback

@app.callback(
Output(‘path’, ‘children’),
Input(‘origin_div’, ‘value’)

But I cannot find how to load the Dataframe (df = pd.read_csv('path') ) into th function

def destination_func():
    return html.Div(
        children=[
            dcc.Markdown('Select country of destination'),
            dcc.Dropdown(
                id='destination_div',
                options=[{'label': i, 'value': i} for i in df['Destination'].drop_duplicates()],
                value=df['Destination'].drop_duplicates()[0]
            )
        ]
    )

Hi crocefisso,

I think something like this should work:


import dash
from dash.exceptions import PreventUpdate
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import pandas as pd
import dash_table

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

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
        dcc.Markdown('Select country'),                              
        dcc.Dropdown(                                                             
            id='origin_div',                                                      
            options=[{'label': 'Malta', 'value': './data/malta.csv'}, {'label': 'Japan', 'value': './data/japan.csv'}],
            value='./data/malta.csv'                                  
        ),
    html.Div(id='table')
])



@app.callback(
    [Output('table', 'children')],
    [Input("origin_div", "value")]
)
def update_table(path):
    df = pd.read_csv(path)
    table = dash_table.DataTable(
            id='table01',
            columns=[{"name": i, "id": i, 'type': 'any'} for i in df.columns],
            data=df.to_dict('records'),
                        )

    
    return table


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

Hello, Eduardo, I need the table to be loaded as a pandas dataframe, is it not possible to avoid using dash table?

 df = pd.read_csv(path)

df is a Pandas Dataframe

The code you posted gives me the following error message:

:stop_sign: Errors ( 1 )

:rescue_worker_helmet:Callback error updating table.children

dash.exceptions.InvalidCallbackReturnValue: The callback …table.children… is a multi-output.
Expected the output type to be a list or tuple but got:
DataTable(columns=[{‘name’: ‘Destination’, ‘id’: ‘Destination’, ‘type’: ‘any’}, {‘name’:

Hi crocefisso,

Sorry, you need to replace this line:

[Output('table', 'children')],

For this line:

Output('table', 'children'),

The [ ] in the Output is it used for more than one output. :woozy_face: