Search code examples
pythonipywidgets

How to get the path of a file upload on ipywidgets?


How can I get the actual path of the file uploaded from the FileUpload widget? In my case, I am uploading a binary file and just need to know where it is so I can process it using an internal application.

When I iterate on the FileUpload value I see there is a file name as a key and a dict as the value:

up_value = self.widgets['file_upload'].value
for file_name, file_dict in up_value.items():
    print(file_dict.keys()) # -> dict_keys(['metadata', 'content'])
    print(file_dict['metadata'].keys()) # -> dict_keys(['name', 'type', 'size', 'lastModified'])

I know the content of the file is uploaded but really don't need that. Also not sure how I would pass that content to my internal processing application. I just want to create a dict that stores the filename as a key and the file path as the value.

thx


Solution

  • Once 'uploaded', the file exists as data in memory, so there is no concept of the originating 'path' in the widget.

    If you want to read or write the file, access the up_value.value[0].content attribute. Your processing application needs to be able to handle bytes input, or you could wrap the data in a BytesIO object.