Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Installing the SageMath Jupyter Kernel and Extensions
Kernels have to register themselves with Jupyter so that they appear in the Jupyter notebook's kernel drop-down. This is done by :class:`SageKernelSpec`. """
SAGE_DOC, SAGE_LOCAL, SAGE_EXTCODE, SAGE_VERSION )
""" Utility to manage SageMath kernels and extensions
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec._display_name # random output 'SageMath 6.9' """
""" Create necessary parent directories
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec._mkdirs() sage: os.path.isdir(spec.nbextensions_dir) True """ raise
def identifier(cls): """ Internal identifier for the SageMath kernel
OUTPUT: the string ``"sagemath"``.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: SageKernelSpec.identifier() 'sagemath' """
""" Symlink ``src`` to ``dst``
This is not an atomic operation.
Already-existing symlinks will be deleted, already existing non-empty directories will be kept.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: path = tmp_dir() sage: spec.symlink(os.path.join(path, 'a'), os.path.join(path, 'b')) sage: os.listdir(path) ['b'] """ return
""" Symlink SageMath's Mathjax install to the Jupyter notebook.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec.use_local_mathjax() sage: mathjax = os.path.join(spec.nbextensions_dir, 'mathjax') sage: os.path.isdir(mathjax) True """
""" Symlink jsmol to the Jupyter notebook.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec.use_local_jsmol() sage: jsmol = os.path.join(spec.nbextensions_dir, 'jsmol') sage: os.path.isdir(jsmol) True """
""" Symlink threejs to the Jupyter notebook.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec.use_local_threejs() sage: threejs = os.path.join(spec.nbextensions_dir, 'threejs') sage: os.path.isdir(threejs) True """
""" Helper to construct the SageMath kernel command.
OUTPUT:
List of strings. The command to start a new SageMath kernel.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec._kernel_cmd() ['/.../sage', '--python', '-m', 'sage.repl.ipython_kernel', '-f', '{connection_file}'] """ os.path.join(SAGE_LOCAL, 'bin', 'sage'), '--python', '-m', 'sage.repl.ipython_kernel', '-f', '{connection_file}', ]
""" Return the kernel spec as Python dictionary
OUTPUT:
A dictionary. See the Jupyter documentation for details.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec.kernel_spec() {'argv': ..., 'display_name': 'SageMath ...'} """ argv=self._kernel_cmd(), display_name=self._display_name, )
""" Install the SageMath Jupyter kernel
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec._install_spec() # not tested """ jsonfile = os.path.join(self.kernel_dir, "kernel.json") import json with open(jsonfile, 'w') as f: json.dump(self.kernel_spec(), f)
""" Symlink miscellaneous resources
This method symlinks additional resources (like the SageMath documentation) into the SageMath kernel directory. This is necessary to make the help links in the notebook work.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec._install_spec() # not tested sage: spec._symlink_resources() # not tested """ path = os.path.join(SAGE_EXTCODE, 'notebook-ipython') for filename in os.listdir(path): self.symlink( os.path.join(path, filename), os.path.join(self.kernel_dir, filename) ) self.symlink( os.path.join(SAGE_DOC, 'html', 'en'), os.path.join(self.kernel_dir, 'doc') )
def update(cls): """ Configure the Jupyter notebook for the SageMath kernel
This method does everything necessary to use the SageMath kernel, you should never need to call any of the other methods directly.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec sage: spec = SageKernelSpec() sage: spec.update() # not tested """ instance = cls() instance.use_local_mathjax() instance.use_local_jsmol() instance.use_local_threejs() instance._install_spec() instance._symlink_resources()
""" Check that we have all prerequisites to run the Jupyter notebook.
In particular, the Jupyter notebook requires OpenSSL whether or not you are using https. See :trac:`17318`.
INPUT:
``debug`` -- boolean (default: ``True``). Whether to print debug information in case that prerequisites are missing.
OUTPUT:
Boolean.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import have_prerequisites sage: have_prerequisites(debug=False) in [True, False] True """ except ImportError: if debug: import traceback traceback.print_exc() return False |