Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FileUpload widget in jupyter lab?

I want to use the FileUpload widget in jupyter lab. I have the following lines of codes 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 ?

like image 544
Statistic Dean Avatar asked Aug 02 '20 12:08

Statistic Dean


People also ask

How do I get Ipywidgets to work in Jupyter lab?

Simply install the python ipywidgets package with pip (pip install ipywidgets==7.6. 0) or conda/mamba (conda install -c conda-forge ipywidgets=7.6. 0) and ipywidgets will automatically work in classic Jupyter Notebook and in JupyterLab 3.0.

What is Ipywidgets package?

IPyWidgets is a Python library of HTML interactive widgets for Jupyter notebook.


Video Answer


2 Answers

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

like image 179
Stefan Avatar answered Oct 11 '22 20:10

Stefan


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.

like image 32
ptyshevs Avatar answered Oct 11 '22 19:10

ptyshevs