Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
elz Avatar asked Nov 13 '17 16:11

elz


People also ask

Can I use Google colab instead of Jupyter Notebook?

Both Jupyter Notebook and Google Colab may be the right choice in particular circumstances. Google Colab is an excellent choice for the entry-level developer or the non-programmer who wants to get started fast without having to install anything.

How do I display output in Google Colab?

Working with Google Sheets Colab also supports rich outputs such as charts. Type in the following code in the Code cell. Note that the graphical output is shown in the output section of the Code cell. Likewise, you will be able to create and display several types of charts throughout your program code.

Can we import .ipynb file in Google Colab?

You can open a new Colab notebook from your Google Drive window or by visiting the Colab site. From the Colaboratory site, you can use the menu to upload an iPython notebook: This will upload the ipynb file that you downloaded before.


2 Answers

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.

like image 136
Craig Citro Avatar answered Oct 23 '22 20:10

Craig Citro


!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)
like image 3
njogholo Avatar answered Oct 23 '22 21:10

njogholo