30

I am trying to use ipywidgets with Google Colaboratory, and (as with plotly) the simplest example from the docs does not work. The code below shows a slider in a local notebook but only returns 10 and <function __main__.f> in a Google notebook.

!pip install ipywidgets

from ipywidgets import interact

def f(x):
  return x

interact(f, x=10)

Is there another custom initialization that I could use to enable the widgets?

elz
  • 5,338
  • 3
  • 28
  • 30
  • Hello, Have you found some solution? I have the same problem – Alejandro Quintanar Feb 14 '18 at 14:20
  • Unfortunately no solution yet :( – elz Feb 14 '18 at 14:55
  • I found how to enable the nbextension, but still not working `!pip install widgetsnbextension !jupyter nbextension install --debug /usr/local/lib/python3.6/dist-packages/widgetsnbextension !jupyter nbextension enable --debug /usr/local/lib/python3.6/dist-packages/widgetsnbextension !jupyter nbextension list` – Alejandro Quintanar Feb 14 '18 at 15:18
  • @AlejandroQH probably, somehow, the Jupyter server has to be restarted after installation/enabling of the module. – Dror Feb 20 '18 at 09:49

5 Answers5

18

Update 2: core ipywidgets now work in Colab, as do many custom widgets! In particular, the base, controls, FileUpload, Image, and output widgets all work in colab. See https://github.com/googlecolab/colabtools/issues/498 for more details.

(Tweaked original answer): ipywidgets don't Just Work with Colab: we have a different security model, wherein each output is in its own iframe (with a different origin than the main Colab page). This prevents ipywidgets from working without changes on the Colab side.

Craig Citro
  • 6,505
  • 1
  • 31
  • 28
3
!pip install ipywidgets

# this will allow the notebook to reload/refresh automatically within the runtime
%reload_ext autoreload
%autoreload 2

from ipywidgets import interact

def f(x):
  return x

interact(f, x=10)
bnjogholo
  • 31
  • 2
2

I think now Ipywidgets is working with Google Collaboratory. I tested some decorators and it ran smoothly.

Your code has resulted in:

enter image description here

dasilvadaniel
  • 413
  • 4
  • 8
  • @dasilvadaniel It is still not working for me, the cell is run, but there is no plot displayed. When I type `!jupyter --version` I get the output: `jupyter core : 4.5.0 jupyter-notebook : 5.2.2 qtconsole : 4.5.2 ipython : 5.5.0 ipykernel : 4.6.1 jupyter client : 5.3.1 jupyter lab : not installed nbconvert : 5.5.0 ipywidgets : 7.5.0 nbformat : 4.4.0 traitlets : 4.3.2 `. What are your configurations? – NeStack Aug 04 '19 at 22:47
  • I'm afraid to say that I have the same settings as yours: jupyter core : 4.5.0 jupyter-notebook : 5.2.2 qtconsole : 4.5.2 ipython : 5.5.0 ipykernel : 4.6.1 jupyter client : 5.3.1 jupyter lab : not installed nbconvert : 5.5.0 ipywidgets : 7.5.0 nbformat : 4.4.0 traitlets : 4.3.2 My runtime time is Python3. Could you check that? Maybe there is something with your cookies or something else related to your browser? – dasilvadaniel Aug 07 '19 at 19:47
1

As of 2021-12 the ipywidgets (widgets) work perfectly in Colab. Well, maybe not 100% of them but a pretty decent amount.

All you need is to import them (in whatever way you prefer, 2 examples below)

import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed

And use (simple example)

widgets.Select(
    options=['Linux', 'Windows', 'macOS'],
    value='macOS',
    # rows=10,
    description='OS:',
    disabled=False
)

enter image description here

You have fantastic documentation here https://ipywidgets.readthedocs.io

Rub
  • 2,071
  • 21
  • 37
0

ipywidgets are kinda-supported in colab now; a notable exception is ipywidgets.Image. See https://github.com/googlecolab/colabtools/issues/587.

Fil
  • 1,766
  • 1
  • 15
  • 15
  • Yeah, image not working is a set back for me as I am trying to build some simple image labeler to assist manual labeling of images to construct a new dataset – kawingkelvin Apr 18 '20 at 19:52