Black Lives Matter. Please consider donating to Black Girls Code today.

Upload component - file not found if not in cwd

Hello,

when I try to upload an Excel file to my dash app through the dcc.Upload component, if the file is not already present in the current working directory, then the system outputs an error message FileNotFoundError: [Errno 2] No such file or directory

My goal here is to allow a user to upload a file and to visualize the graph of that file’s contents on the go.

Can anyone help me out? Thanks!

My code is the following:

app.layout = html.Div(
            children=[  
            html.H2('1. Upload a file', style={'margin': '10px'}),
            html.P(instructions_1, style={'margin': '10px'}),
            dcc.Upload(
                    id='upload-data',
                    children=html.Div([
                        'Select a .xlsx file',
                    ]),

...

@forecaster.callback(Output('graph', 'figure'), 
              [Input('upload-data', 'filename')],
              events = [Event('update', 'interval')])   
def update_graph(file):

if file != None:        
    with open(file):
        df = pd.read_excel(file)

`

The normal usage of the upload component is to send the contents of the file through the callback. In other words, you shouldn’t have to do a read_excel file. We do it this way so that the app works when you deploy it: the filename of the file that the user uploads will not be on the system of the server, and so pd.read_excel won’t work.

Got it. But how do I create a pandas dataframe off that file?

There is an example of this in our docs here: https://dash.plot.ly/dash-core-components/upload

Thanks, your previous explanation had me understand the logic behind the component. Fixed!

1 Like