How to use FileUpload widget in jupyter lab?

For me it worked after

pip install jupyterlab-widgets
jupyter labextension install @jupyter-widgets/jupyterlab-manager

Also see

https://developer.aliyun.com/mirror/npm/package/@jupyter-widgets/jupyterlab-manager

Usage

from ipywidgets import FileUpload
from IPython.display import display
upload = FileUpload(accept='.txt', multiple=True)
display(upload)

with open('z_merged_output.txt', 'wb') as output_file: 
    for uploaded_filename in upload.value:
        content = upload.value[uploaded_filename]['content']   
        output_file.write(content) 

enter image description here


If you're using jupyterlab out the box, it doesn't have ipywidgets enabled by default, you need to rebuild it after enabling the extension. Follow the steps from here:

  1. Install nodeJS
  2. pip install ipywidgets
  3. jupyter nbextension enable --py widgetsnbextension
  4. jupyter labextension install @jupyter-widgets/jupyterlab-manager
  5. (may need to restart your lab)

It says that newer Jupyterlab has it enabled, but I still had troubles with it, depending on the platform. Manual install is usually the way to go.