elz
elz

Reputation: 5698

Ipywidgets with Google Colaboratory

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?

Upvotes: 31

Views: 17554

Answers (5)

Rub
Rub

Reputation: 2708

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

Upvotes: 1

Craig Citro
Craig Citro

Reputation: 6625

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.

Upvotes: 19

dasilvadaniel
dasilvadaniel

Reputation: 463

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

Upvotes: 2

bnjogholo
bnjogholo

Reputation: 31

!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)

Upvotes: 3

Fil
Fil

Reputation: 1834

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

Upvotes: 0

Related Questions