Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
3-manifolds
GitHub Repository: 3-manifolds/Sage_macOS
Path: blob/main/Sage_framework/files/ipython_kernel/kernel.py
173 views
1
# sage_setup: distribution = sagemath-repl
2
"""
3
The Sage ZMQ Kernel
4
5
Version of the Jupyter kernel when running Sage inside the Jupyter
6
notebook or remote Jupyter sessions.
7
"""
8
9
# ***************************************************************************
10
# Copyright (C) 2015 Volker Braun <[email protected]>
11
#
12
# Distributed under the terms of the GNU General Public License (GPL)
13
# as published by the Free Software Foundation; either version 2 of
14
# the License, or (at your option) any later version.
15
# https://www.gnu.org/licenses/
16
# ***************************************************************************
17
18
import sys
19
import warnings
20
with warnings.catch_warnings():
21
# When upstream pydevd (as opposed to the bundled version) is used
22
# with debugpy, a PEP 420 warning is emitted. Debugpy and/or
23
# pydevd will eventually work around this, but as of September
24
# 2023, hiding the warning gives us more flexibility in the
25
# versions of those packages that we can accept.
26
warnings.filterwarnings("ignore",
27
message=r".*pkg_resources\.declare_namespace",
28
category=DeprecationWarning)
29
from ipykernel.ipkernel import IPythonKernel
30
31
from ipykernel.zmqshell import ZMQInteractiveShell
32
from traitlets import Type
33
34
from sage.env import SAGE_VERSION
35
from sage.repl.interpreter import SageNotebookInteractiveShell
36
from sage.repl.ipython_extension import SageJupyterCustomizations
37
38
39
class SageZMQInteractiveShell(SageNotebookInteractiveShell, ZMQInteractiveShell):
40
pass
41
42
43
class SageKernel(IPythonKernel):
44
implementation = 'sage'
45
implementation_version = SAGE_VERSION
46
47
shell_class = Type(SageZMQInteractiveShell)
48
49
def __init__(self, **kwds):
50
"""
51
The Sage Jupyter Kernel
52
53
INPUT:
54
55
See the Jupyter documentation
56
57
EXAMPLES::
58
59
sage: from sage.repl.ipython_kernel.kernel import SageKernel
60
sage: SageKernel.__new__(SageKernel)
61
<sage.repl.ipython_kernel.kernel.SageKernel object at 0x...>
62
"""
63
super().__init__(**kwds)
64
SageJupyterCustomizations(self.shell)
65
66
@property
67
def banner(self):
68
r"""
69
The Sage Banner
70
71
The value of this property is displayed in the Jupyter
72
notebook.
73
74
OUTPUT:
75
76
String.
77
78
EXAMPLES::
79
80
sage: from sage.repl.ipython_kernel.kernel import SageKernel
81
sage: sk = SageKernel.__new__(SageKernel)
82
sage: print(sk.banner)
83
┌...SageMath version...
84
"""
85
from sage.misc.banner import banner_text
86
return banner_text()
87
88
@property
89
def help_links(self):
90
r"""
91
Help in the Jupyter Notebook
92
93
OUTPUT:
94
95
See the Jupyter documentation.
96
97
EXAMPLES::
98
99
sage: from sage.repl.ipython_kernel.kernel import SageKernel
100
sage: sk = SageKernel.__new__(SageKernel)
101
sage: sk.help_links
102
[{'text': 'Sage Documentation',
103
'url': 'http://127.0.0.1:XXXXX'},
104
...]
105
"""
106
try:
107
doc_url = 'http://%s:%s'%self.docserver.address()
108
except:
109
doc_url = 'https://doc.sagemath.org/html/en'
110
# This works with Classic Jupyter and Jupyter Lab. Not with V7.
111
return [
112
{
113
'text': 'Sage Documentation',
114
'url': doc_url,
115
},
116
{
117
'text': 'Sage Reference',
118
'url': doc_url + '/reference/',
119
},
120
{
121
'text': 'Sage Tutorial',
122
'url': doc_url + '/tutorial/',
123
},
124
{
125
'text': 'Python',
126
'url': 'http://docs.python.org/%i.%i' % sys.version_info[:2],
127
},
128
{
129
'text': 'IPython',
130
'url': 'http://ipython.org/documentation.html',
131
},
132
{
133
'text': 'Singular',
134
'url': 'http://www.singular.uni-kl.de/Manual/latest/index.htm',
135
},
136
{
137
'text': 'GAP',
138
'url': 'http://gap-system.org/Manuals/doc/ref/chap0.html',
139
},
140
{
141
'text': 'NumPy',
142
'url': 'http://docs.scipy.org/doc/numpy/reference/',
143
},
144
{
145
'text': 'SciPy',
146
'url': 'http://docs.scipy.org/doc/scipy/reference/',
147
},
148
{
149
'text': 'SymPy',
150
'url': 'http://docs.sympy.org/latest/index.html',
151
},
152
{
153
'text': 'Matplotlib',
154
'url': 'https://matplotlib.org/contents.html',
155
},
156
{
157
'text': 'Markdown',
158
'url': 'http://help.github.com/articles/github-flavored-markdown',
159
},
160
]
161
162
def pre_handler_hook(self):
163
"""
164
Restore the signal handlers to their default values at Sage
165
startup, saving the old handler at the ``saved_sigint_handler``
166
attribute. This is needed because Jupyter needs to change the
167
``SIGINT`` handler.
168
169
See :issue:`19135`.
170
171
TESTS::
172
173
sage: from sage.repl.ipython_kernel.kernel import SageKernel
174
sage: k = SageKernel.__new__(SageKernel)
175
sage: k.pre_handler_hook()
176
sage: k.saved_sigint_handler
177
<cyfunction python_check_interrupt at ...>
178
"""
179
from cysignals import init_cysignals
180
self.saved_sigint_handler = init_cysignals()
181
182