Ipywidgets with Google Colaboratory

Viewed 12000

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?

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

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

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

Related