Home > Mobile >  'float' object not iterable, dcc.Upload in Dash
'float' object not iterable, dcc.Upload in Dash

Time:10-21

This is a strange error, and one that has been asked before picture of my web app

When I load the app it runs as expected, I can add rows, but when I upload a csv file I get the following error:

File "C:\Users\harry\OneDrive\Documents\coding\fypWebApp\dashApp\app.py", line 255, in update_output
zip(list_of_contents, list_of_names, list_of_dates)]
TypeError: 'float' object is not iterable

It seems the problem lies in one of the lists in the update_output function. Also, I get the following notice in VSCode.

"content_type" is not accessed

This notice does not appear when I have just the code taken from the documentation in a standalone file (as seen in the image below), only when I enter the code into my code.

same code, no error

I don't know where to start to fix this problem, and it seems like it requires a good understanding of Dash to solve. Any help is appreciated.

CodePudding user response:

After long debugging, the problem is that list_of_dates is a float number which represents the last modified for the uploaded file. There is a list comprehension in the callback which iterates through this float number, which does not make sense to iterate through a number, and it eventually throws an error. To solve this problem, all you should do is to replace the callback function with the following:

def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [parse_contents(list_of_contents, list_of_names, list_of_dates)]
        return children

Inside the function parse_contents, I added the delimiter=";" to read the CSV file properly.

 if 'csv' in filename:
            # Assume that the user uploaded a CSV file
            df = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')), delimiter=";") #<-- this line.

Finally, I reduced the size of the upper table to show the table of the CSV file.

Output

enter image description here

  • Related