18

I want to use the FileUpload widget in jupyter lab. I have the following lines of code in my notebook cell:

uploader = widgets.FileUpload()
uploader

In jupyter notebook, the output of the cell is a clickable button that I can use to upload a file. In jupyter lab, the output is the following :

FileUpload(value={}, description='Upload')

Here's the info on the uploader object :

Type:           FileUpload
String form:    FileUpload(value={}, description='Upload')
File:           ~/miniconda3/envs/fastai2/lib/python3.7/site-packages/ipywidgets/widgets/widget_upload.py

Is it possible to make this widget work on jupyter lab? And if so how should I proceed ?

starball
  • 20,030
  • 7
  • 43
  • 238
Statistic Dean
  • 4,861
  • 7
  • 22
  • 46

4 Answers4

14

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.

ptyshevs
  • 1,602
  • 11
  • 26
  • I think the step I was missing was step 3 or 4, after following your instructions, everything worked perfectly. Thank you ! – Statistic Dean Aug 06 '20 at 10:10
  • 2
    @StatisticDean, yeah, it is understandably not obvious, because usually installing a module is sufficient. It's not the case with JupyterLab, though. Every time you install an extension or update to it, it requires the rebuild. – ptyshevs Aug 06 '20 at 10:14
5

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

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

If you've already got ipywidgets installed you may need to update it:

pip install -U ipywidgets

And then install the newer version's files:

jupyter nbextension install --py widgetsnbextension
Pierz
  • 7,064
  • 52
  • 59
0

For me, it worked after running the below code

!jupyter nbextension enable --py --sys-prefix widgetsnbextension
!jupyter nbextension enable fileupload --user --py
(doesnot require restarting of kernel)
starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '22 at 11:56