6

I'm new to both Sage and Python. For writing sage program, I went to sagemath cloud, and when I tried to create a new file, I had, among others, two options: sage worksheet and IPython notebook. I noticed both does coloring and indentation. But I was wondering: is there any way to take advantage of both of them together? How can I combine both of them together? For example, in IPython notebook, how can I make it understand the sage commands?

To give an explicit and concrete example of the above, in Sage tutorials, there's factor(-2007). If you run it on sage worksheet, it gives (as you expect), -1*3^2*223, but in IPython notebook, it shows 'factor' is not defined. How can I write factor(-2007) in IPynb, run it, and get -1*3^2*223?

I'm working in a windows 8, 64 bit environment, just if it is relevant.

  • The following question may help you, though I think some of it may be outdated. http://stackoverflow.com/questions/18412386/ipython-sage-dependencies-anaconda-and-package-integration – kcrisman Apr 30 '14 at 12:45

2 Answers2

10

On a machine with everything properly configured, e.g., https://cloud.sagemath.com, you can just type

%load_ext sage

into an IPython notebook cell, and then you can use Sage (except plotting graphics might not work yet). I've attached a screenshot showing this. (NOTE: I've edited this answer, but not the screenshot -- the extension used to be more complicated.)

enter image description here

William Stein
  • 2,465
  • 1
  • 18
  • 12
  • 1
    I'm a sagemath.com newb but it appears to me that this should now be: %load_ext sage which is what's suggested under the 'i' icon in the iPyNB page. – Bill Bell Oct 11 '14 at 18:01
  • 1
    Hi -- when I wrote that answer it was write. However, it has since *changed* to `%load_ext sage`, which is now the right answer. – William Stein Oct 12 '14 at 00:19
1

Note: the IPython notebook is now the Jupyter notebook.

To use Sage in a Jupyter notebook worksheet, you can either

  • use the Python2 kernel, and start with %load_ext sage, or
  • use the SageMath kernel, and then you don't need %load_ext sage.

To change kernel, use the Kernel > Change Kernel menu item, from the menu bar that has File, Edit, View, Insert, Kernel, Help.

Also, there is now a native SageMath installer for Windows, which you can get from

and which works with any 64-bit Windows.

This will let you use SageMath using the Sage REPL (or command-line interface), or the Jupyter notebook, or the legacy SageNB notebook.

You should also be able to install JupyterLab by opening a Sage shell and running sage --pip install jupyterlab, and then you can launch JupyterLab by running sage -n jupyterlab.

Similarly, sage -n jupyter gives you the Jupyter notebook, and sage -n sagenbgives you the legacy SageNB notebook.

All this also works on CoCalc, the new name of SageMathCloud.

Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27