Ask Your Question

SageMath and interact / ipywidgets with jupyter

asked 2018-02-26 19:16:26 +0100

j.c. gravatar image

updated 2018-02-26 21:12:31 +0100

I got the SageMath kernel working well inside my anaconda distribution's copy of jupyter, following these instructions, so I can open SageMath notebooks using anaconda jupyter and they run perfectly fine -- almost.

I would like to get interact elements / ipywidgets working when I use SageMath as a kernel in anaconda jupyter, but with my current configuration (hopefully described in sufficient detail below), I get the following error message in the console

[IPKernelApp] WARNING | Widget Javascript not detected. It may not be installed or enabled properly.

and the notebook simply outputs the initial value of the function with no interactivity.

I'm running SageMath 8.1 on macOS. I've been able to get interact elements and ipywidgets with SageMath code running in the SageMath jupyter notebook interface (not sure if this was necessary, but I ran

/local/bin/jupyter nbextension enable --py widgetsnbextension

in the SageMath directory.) Everything works as expected, i.e. if I run sagemath -n jupyter and open a notebook containing:

import ipywidgets as widgets

widgets.IntSlider( value=7, min=0, max=10, step=1, description='Test:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='d' )

I get a slider widget which works.

Similarly, examples from this interact tutorial on the SageMath wiki run perfectly fine in SageMath jupyter.

I also have an up-to-date anaconda install of jupyter where the ipywidgets nbextension has been enabled; the above ipywidgets code can be run in anaconda jupyter with the ordinary python kernel and works fine.

[The real reason I am asking this question is I would like to figure out how to get interactive widgets working in jupyterlab with the SageMath kernel, but I'm guessing that if the above question can be answered, I can get everything else working as well.]

edit retag flag offensive close merge delete


Just something to try: does the working directory make a difference? Could you try running the Sage-Jupyter-in-anaconda from the Sage root directory?

Jeroen Demeyer gravatar imageJeroen Demeyer ( 2018-03-01 10:02:56 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-03-02 00:03:36 +0100

j.c. gravatar image

updated 2018-03-02 00:08:03 +0100

I asked this question on the ipywidgets github page and was able to solve this issue.

In short, as explained to me on github, the version of ipywidgets that comes installed with SageMath 8.1 (version 6.0.0) is too old to interface with the widgetsnbextension that anaconda jupyter was using. The solution was to upgrade SageMath's ipywidgets package.

I first had to install ssl in sage first, which I did by following the instructions in this thread. I ran the following commands in order:

sage -i openssl

sage -f python2

sage -i pyopenssl

After about 2 hours of rebuilding, this step was complete and then I ran the following pip command to upgrade ipywidgets and all of its dependencies for Sage: sage -pip install --upgrade ipywidgets

This completed successfully, so I did some testing and ipywidgets (and interact) elements now work perfectly in my anaconda jupyter notebook when I use the SageMath kernel.

They also work when I use the SageMath kernel in jupyterlab as well.

edit flag offensive delete link more


Well done. You should accept your own answer to mark your question as solved.

slelievre gravatar imageslelievre ( 2018-03-02 15:14:44 +0100 )edit

Note that a more recent version of ipywidgets should make its way into Sage 8.2.

See Sage trac ticket #23177: upgrade to ipywidgets 7.

You can already try it out by installing the latest development release of Sage, Sage 8.2.beta6.

slelievre gravatar imageslelievre ( 2018-03-02 15:18:13 +0100 )edit

Thanks for the info. I wasn't able to accept my own answer previously since I didn't have 50 points, but now I can.

j.c. gravatar imagej.c. ( 2018-03-02 17:24:10 +0100 )edit

answered 2018-03-01 17:13:57 +0100

Jeroen Demeyer gravatar image

updated 2018-03-01 17:14:09 +0100

And I forgot the obvious: clear your browser cache! You might be using a cached newer version of the widgets JavaScript.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-02-26 19:16:26 +0100

Seen: 1,922 times

Last updated: Mar 02 '18