Ipywidgets: Work with files after using file upload

Created on 22 Aug 2019  路  2Comments  路  Source: jupyter-widgets/ipywidgets

I would like to implement the file upload into my code, although when it is uploaded the file is in binary. How would I, for example, import a vtk file and then use the file for plotting? I'm not sure how to work with the binary.

Thank you!

Most helpful comment

Hi guys, just reading through here. Not sure if this is a permanent solution, but I do think that a lot of people that use notebooks remotely (on aws or a jupyterhub, etc.) would want to be able to fully upload a file from a client device to a directory local to the jupyter kernel in addition to those using the data in python.

Here is the approach that I took, and some notes:

myupload = widgets.FileUpload(
    accept= '.txt', 
    multiple=False  
)
myupload
# This shows the uploader button and allows me to upload the filename
# I uploaded a local textfile from my client PC named local_file.txt
# You can check myupload.keys, but there is no key for filename. 

# So to grab the filename, you can check what is in the value dict.
myupload.value

Returns the value dictionary: 
{'local_file.txt': {'metadata': {'name': 'local_file.txt',
   'type': 'text/plain',
   'size': 75,
   'lastModified': 1568824413185},
  'content': b'\nSome text here in a text file on my local PC directory, ready for upload.\n'}}

# Unfortunately, the filename shows up as a key, not as a value.  
# Also - very importantly, if I upload two or more files, I only get the most recent filename and value in the dict.  

# So, to get the filename, and get the file content, I used a derivative of what Jason did:
uploaded_filename = next(iter(myupload.value))
content = myupload.value[uploaded_filename]['content']
with open('myfile', 'wb') as f: f.write(content)

## This works, and drops the file onto the server.  
## Would be nice if the uploaded filename and value were a key, value pair in the value dict. 
## Not sure how to do this with multiple files.  For me, it isn't showing the separate filenames and values in the value dict.

All 2 comments

I think this is a question for vtk. The ipywidget file upload's responsibility is to get the binary content of the file to you in python. How you use the file will totally depend on what you want you want to do with it.

For example, you could save it to a local file using something like with open('myfile', 'wb') as f: f.write(widget.value) (not tested).

Hi guys, just reading through here. Not sure if this is a permanent solution, but I do think that a lot of people that use notebooks remotely (on aws or a jupyterhub, etc.) would want to be able to fully upload a file from a client device to a directory local to the jupyter kernel in addition to those using the data in python.

Here is the approach that I took, and some notes:

myupload = widgets.FileUpload(
    accept= '.txt', 
    multiple=False  
)
myupload
# This shows the uploader button and allows me to upload the filename
# I uploaded a local textfile from my client PC named local_file.txt
# You can check myupload.keys, but there is no key for filename. 

# So to grab the filename, you can check what is in the value dict.
myupload.value

Returns the value dictionary: 
{'local_file.txt': {'metadata': {'name': 'local_file.txt',
   'type': 'text/plain',
   'size': 75,
   'lastModified': 1568824413185},
  'content': b'\nSome text here in a text file on my local PC directory, ready for upload.\n'}}

# Unfortunately, the filename shows up as a key, not as a value.  
# Also - very importantly, if I upload two or more files, I only get the most recent filename and value in the dict.  

# So, to get the filename, and get the file content, I used a derivative of what Jason did:
uploaded_filename = next(iter(myupload.value))
content = myupload.value[uploaded_filename]['content']
with open('myfile', 'wb') as f: f.write(content)

## This works, and drops the file onto the server.  
## Would be nice if the uploaded filename and value were a key, value pair in the value dict. 
## Not sure how to do this with multiple files.  For me, it isn't showing the separate filenames and values in the value dict.
Was this page helpful?
0 / 5 - 0 ratings