27

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
  • 3,581
  • 2
  • 21
  • 28
  • 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

4 Answers4

15

Update: core ipywidgets now work in Colab! In particular, the base, controls, FileUpload, Image, and output widgets all work in colab. Third-party widgets still do not work.

(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,046
  • 1
  • 26
  • 27
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 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
!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)
njogholo
  • 11
  • 1
0

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

Fil
  • 1,518
  • 1
  • 14
  • 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