Path: blob/main/Sage_framework/files/ipython_kernel/kernel.py
173 views
# sage_setup: distribution = sagemath-repl1"""2The Sage ZMQ Kernel34Version of the Jupyter kernel when running Sage inside the Jupyter5notebook or remote Jupyter sessions.6"""78# ***************************************************************************9# Copyright (C) 2015 Volker Braun <[email protected]>10#11# Distributed under the terms of the GNU General Public License (GPL)12# as published by the Free Software Foundation; either version 2 of13# the License, or (at your option) any later version.14# https://www.gnu.org/licenses/15# ***************************************************************************1617import sys18import warnings19with warnings.catch_warnings():20# When upstream pydevd (as opposed to the bundled version) is used21# with debugpy, a PEP 420 warning is emitted. Debugpy and/or22# pydevd will eventually work around this, but as of September23# 2023, hiding the warning gives us more flexibility in the24# versions of those packages that we can accept.25warnings.filterwarnings("ignore",26message=r".*pkg_resources\.declare_namespace",27category=DeprecationWarning)28from ipykernel.ipkernel import IPythonKernel2930from ipykernel.zmqshell import ZMQInteractiveShell31from traitlets import Type3233from sage.env import SAGE_VERSION34from sage.repl.interpreter import SageNotebookInteractiveShell35from sage.repl.ipython_extension import SageJupyterCustomizations363738class SageZMQInteractiveShell(SageNotebookInteractiveShell, ZMQInteractiveShell):39pass404142class SageKernel(IPythonKernel):43implementation = 'sage'44implementation_version = SAGE_VERSION4546shell_class = Type(SageZMQInteractiveShell)4748def __init__(self, **kwds):49"""50The Sage Jupyter Kernel5152INPUT:5354See the Jupyter documentation5556EXAMPLES::5758sage: from sage.repl.ipython_kernel.kernel import SageKernel59sage: SageKernel.__new__(SageKernel)60<sage.repl.ipython_kernel.kernel.SageKernel object at 0x...>61"""62super().__init__(**kwds)63SageJupyterCustomizations(self.shell)6465@property66def banner(self):67r"""68The Sage Banner6970The value of this property is displayed in the Jupyter71notebook.7273OUTPUT:7475String.7677EXAMPLES::7879sage: from sage.repl.ipython_kernel.kernel import SageKernel80sage: sk = SageKernel.__new__(SageKernel)81sage: print(sk.banner)82┌...SageMath version...83"""84from sage.misc.banner import banner_text85return banner_text()8687@property88def help_links(self):89r"""90Help in the Jupyter Notebook9192OUTPUT:9394See the Jupyter documentation.9596EXAMPLES::9798sage: from sage.repl.ipython_kernel.kernel import SageKernel99sage: sk = SageKernel.__new__(SageKernel)100sage: sk.help_links101[{'text': 'Sage Documentation',102'url': 'http://127.0.0.1:XXXXX'},103...]104"""105try:106doc_url = 'http://%s:%s'%self.docserver.address()107except:108doc_url = 'https://doc.sagemath.org/html/en'109# This works with Classic Jupyter and Jupyter Lab. Not with V7.110return [111{112'text': 'Sage Documentation',113'url': doc_url,114},115{116'text': 'Sage Reference',117'url': doc_url + '/reference/',118},119{120'text': 'Sage Tutorial',121'url': doc_url + '/tutorial/',122},123{124'text': 'Python',125'url': 'http://docs.python.org/%i.%i' % sys.version_info[:2],126},127{128'text': 'IPython',129'url': 'http://ipython.org/documentation.html',130},131{132'text': 'Singular',133'url': 'http://www.singular.uni-kl.de/Manual/latest/index.htm',134},135{136'text': 'GAP',137'url': 'http://gap-system.org/Manuals/doc/ref/chap0.html',138},139{140'text': 'NumPy',141'url': 'http://docs.scipy.org/doc/numpy/reference/',142},143{144'text': 'SciPy',145'url': 'http://docs.scipy.org/doc/scipy/reference/',146},147{148'text': 'SymPy',149'url': 'http://docs.sympy.org/latest/index.html',150},151{152'text': 'Matplotlib',153'url': 'https://matplotlib.org/contents.html',154},155{156'text': 'Markdown',157'url': 'http://help.github.com/articles/github-flavored-markdown',158},159]160161def pre_handler_hook(self):162"""163Restore the signal handlers to their default values at Sage164startup, saving the old handler at the ``saved_sigint_handler``165attribute. This is needed because Jupyter needs to change the166``SIGINT`` handler.167168See :issue:`19135`.169170TESTS::171172sage: from sage.repl.ipython_kernel.kernel import SageKernel173sage: k = SageKernel.__new__(SageKernel)174sage: k.pre_handler_hook()175sage: k.saved_sigint_handler176<cyfunction python_check_interrupt at ...>177"""178from cysignals import init_cysignals179self.saved_sigint_handler = init_cysignals()180181182