FYI : ipywidgets allows (sort of) interactive Sage graphs in Jupyter notebook.

96 views
Skip to first unread message

Emmanuel Charpentier

unread,
Sep 20, 2016, 10:12:26 AM9/20/16
to sage-support
I found this useful (at least as a stopgap measure while waiting for Trac#21267, which may be not as easy as it seems...), and thought that documenting this might be useful to people wanting interactive displays in Sagemath Jupyter notebooks :

The ipywidgets python package can be installed in the Sage jupyter notebook. To enable installation "in the right place", the installation instructions should be modified as :

sage -pip install ipywidgets
echo "jupyter nbextension enable --py --sys-prefix widgetsnbextension" | sage -sh

if you use the Sage Jupyter notebook (the original installation instructions are enough if you have installed your Sage kernel
in the system's Jupyter notebook). The following code snippet is enough to use the package :

from __future__ import print_function from ipywidgets import interact, interactive, fixed import ipywidgets as widgets

And that's all... My limited testing seems to validate this (ab?)use of the package :

var("x,a,b")
dbeta(x,a,b)=x^(a-1)*(1-x)^(b-1)/beta(a,b)
def showbeta(a,b):
show(plot(dbeta(x,a,b),(x,0,1),figsize=4))
return(None)
interact(showbeta,
a=widgets.FloatSlider(min=0.0,max=10.0,step=0.1,value=1.0,continuous_update=False),
b=widgets.FloatSlider(min=0.0,max=10.0,step=0.1,value=1.0,continuous_update=False));


Note that the ipywidget package has no way to use a lot of Sage data types. Any argument to them have to be cast to
native python data types (int, float, string).

Clunky, but damn useful, at least for pedagogic purposes...

HTH,

--
Emmanuel Charpentier

Jeroen Demeyer

unread,
Sep 20, 2016, 10:27:28 AM9/20/16
to sage-s...@googlegroups.com
On 2016-09-20 16:12, Emmanuel Charpentier wrote:
> sage -pip install ipywidgets
>
> echo "jupyter nbextension enable --py --sys-prefix widgetsnbextension" |
> sage -sh

In recent Sage betas, the above should not be needed.

Emmanuel Charpentier

unread,
Sep 20, 2016, 4:09:21 PM9/20/16
to sage-support

May I ask why ? (Just curious, no hostility whatsoever implied...).

-
Emmanuel Charpentier

Jeroen Demeyer

unread,
Sep 20, 2016, 4:15:43 PM9/20/16
to sage-s...@googlegroups.com
On 2016-09-20 22:09, Emmanuel Charpentier wrote:
> May I ask why ? (Just curious, no hostility whatsoever implied...).

Because those packages are installed and the nbextension is enabled in
Sage by default: https://trac.sagemath.org/ticket/21256

Emmanuel Charpentier

unread,
Sep 20, 2016, 11:30:48 PM9/20/16
to sage-support

Thanks ! I missed this one...

--
Emmanuel Charpentier
 
Reply all
Reply to author
Forward
0 new messages