Skip to content

sagejupyter

Hal Snyder edited this page Jul 21, 2016 · 24 revisions

Sage Worksheets as Jupyter Clients

Starting a Jupyter kernel

Use the jupyter command to launch any installed jupyter kernel from a Sage worksheet

py3 = jupyter("python3")

After that, any cell that begins with %py3 will send statements to the Python3 kernel that you just started. If you want to draw graphics, there is no need to call %matplotlib inline.

%py3
print(42)

import numpy as np; import pylab as plt
x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.show()

You can set the default mode to be your jupyter kernel for all cells in the worksheet: after putting the following in a cell, click the "restart" button, and you have an anaconda worksheet.

%auto
anaconda3 = jupyter('anaconda3')
%default_mode anaconda3

Each call to jupyter() launches its own Jupyter kernel. So you can have more than one instance of the same kernel type in the same worksheet session.

p1 = jupyter('python3')
p2 = jupyter('python3')
p1('a = 5')
p2('a = 10')
p1('print(a)')   # prints 5
p2('print(a)')   # prints 10

Get a version of this help message from within a sage worksheet, regardless of default mode, with

%sage
jupyter?

Supported Features

Features listed below are demonstrated in sample worksheets at
https://github.com/sagemath/cloud-examples/tree/master/sagews-jupyter.

  1. Help, with list of available kernels: jupyter?.
  2. Execution count: [n]: 'XYZ'.
  3. pwd
  4. ls with color-coded output
  5. Plot to png.
  6. Plot to image/svg+xml.
  7. Display image from local file or URL.
  8. Ignore python prompt markers.
  9. Error output with ansi colors.
  10. Non-blocking output.
  11. Function docstring with ansi colors.
  12. Long docstring and magic output to scrolling div.
  13. Markdown, including inline and display MathJAX.
  14. Embed Youtube video.
  15. Embed web page from external site.
  16. LaTeX output from Math() and Latex() objects.
  17. Play audio from local file.
  18. Play audio from generated data.
  19. Autocompletion, if supported by jupyter kernel - tested with python, R, and bash.

Known Issues

  1. Raw input hangs the worksheet: a = raw_input("type here: ").
  2. If default_mode is enabled for a jupyter mode and you would like a cell to start with a jupyter magic command, the first line of the cell should be a comment or mode decorator, or the jupyter magic will be interpreted as a sagews mode.
  3. Long docstring and magic create scrollable div, but there is no close button.
  4. If the jupyter kernel crashes, it must be restarted manually.
  5. %load somefile - hangs the .sagews
  6. Embedded youtube widget is rendered multiple times.
  7. ipywidgets are not supported.
  8. jupyter? calls _get_doc() three times
Clone this wiki locally