Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking advantage of Sage and IPython notebook in the same page? Or, rather combining them?

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.

like image 296
Noprogexprnce mathmtcn Avatar asked Apr 30 '14 09:04

Noprogexprnce mathmtcn


People also ask

What is the difference between Jupyter Notebook and IPython notebook?

Going forward, Jupyter will contain the language-agnostic projects that serve many languages. IPython will continue to focus on Python and its use with Jupyter. Jupyter's architecture includes frontends (web or console) and backends (kernels for various languages). IPython console is only about Python and terminal.

Is IPython notebook and Jupyter Notebook same?

Jupyter Notebook (formerly IPython Notebook) is a web-based interactive computational environment for creating, executing, and visualizing Jupyter notebooks.

Can I use sage in Jupyter Notebook?

For added convenience, it is possible to link your installation of SageMath into your Jupyter installation, adding it to the list of available kernels that can be selected in the notebook or JupyterLab interface. to find the location of the SageMath kernel description.


2 Answers

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

like image 139
William Stein Avatar answered Sep 30 '22 12:09

William Stein


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

  • https://github.com/sagemath/sage-windows/releases

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.

like image 29
Samuel Lelièvre Avatar answered Sep 29 '22 12:09

Samuel Lelièvre