|
@@
-6,6
+6,8
b''
|
|
6
|
|
|
6
|
|
|
7
|
import logging
|
|
7
|
import logging
|
|
8
|
import os
|
|
8
|
import os
|
|
|
|
|
9
|
import signal
|
|
|
|
|
10
|
import sys
|
|
9
|
|
|
11
|
|
|
10
|
import zmq
|
|
12
|
import zmq
|
|
11
|
|
|
13
|
|
|
@@
-16,7
+18,6
b' import tornado.ioloop'
|
|
16
|
tornado.ioloop = ioloop
|
|
18
|
tornado.ioloop = ioloop
|
|
17
|
|
|
19
|
|
|
18
|
from tornado import httpserver
|
|
20
|
from tornado import httpserver
|
|
19
|
from tornado import options
|
|
|
|
|
20
|
from tornado import web
|
|
21
|
from tornado import web
|
|
21
|
|
|
22
|
|
|
22
|
from kernelmanager import KernelManager
|
|
23
|
from kernelmanager import KernelManager
|
|
@@
-28,7
+29,16
b' from handlers import ('
|
|
28
|
from routers import IOPubStreamRouter, ShellStreamRouter
|
|
29
|
from routers import IOPubStreamRouter, ShellStreamRouter
|
|
29
|
|
|
30
|
|
|
30
|
from IPython.core.application import BaseIPythonApplication
|
|
31
|
from IPython.core.application import BaseIPythonApplication
|
|
|
|
|
32
|
from IPython.core.profiledir import ProfileDir
|
|
|
|
|
33
|
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
|
|
31
|
from IPython.zmq.session import Session
|
|
34
|
from IPython.zmq.session import Session
|
|
|
|
|
35
|
from IPython.zmq.zmqshell import ZMQInteractiveShell
|
|
|
|
|
36
|
from IPython.zmq.ipkernel import (
|
|
|
|
|
37
|
flags as ipkernel_flags,
|
|
|
|
|
38
|
aliases as ipkernel_aliases,
|
|
|
|
|
39
|
IPKernelApp
|
|
|
|
|
40
|
)
|
|
|
|
|
41
|
from IPython.utils.traitlets import Dict, Unicode, Int, Any, List, Enum
|
|
32
|
|
|
42
|
|
|
33
|
#-----------------------------------------------------------------------------
|
|
43
|
#-----------------------------------------------------------------------------
|
|
34
|
# Module globals
|
|
44
|
# Module globals
|
|
@@
-37,13
+47,15
b' from IPython.zmq.session import Session'
|
|
37
|
_kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
|
|
47
|
_kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
|
|
38
|
_kernel_action_regex = r"(?P<action>restart|interrupt)"
|
|
48
|
_kernel_action_regex = r"(?P<action>restart|interrupt)"
|
|
39
|
|
|
49
|
|
|
|
|
|
50
|
LOCALHOST = '127.0.0.1'
|
|
|
|
|
51
|
|
|
40
|
#-----------------------------------------------------------------------------
|
|
52
|
#-----------------------------------------------------------------------------
|
|
41
|
# The Tornado web application
|
|
53
|
# The Tornado web application
|
|
42
|
#-----------------------------------------------------------------------------
|
|
54
|
#-----------------------------------------------------------------------------
|
|
43
|
|
|
55
|
|
|
44
|
class NotebookWebApplication(web.Application):
|
|
56
|
class NotebookWebApplication(web.Application):
|
|
45
|
|
|
57
|
|
|
46
|
def __init__(self, kernel_manager, log):
|
|
58
|
def __init__(self, kernel_manager, log, kernel_argv):
|
|
47
|
handlers = [
|
|
59
|
handlers = [
|
|
48
|
(r"/", MainHandler),
|
|
60
|
(r"/", MainHandler),
|
|
49
|
(r"/kernels", KernelHandler),
|
|
61
|
(r"/kernels", KernelHandler),
|
|
@@
-61,7
+73,9
b' class NotebookWebApplication(web.Application):'
|
|
61
|
|
|
73
|
|
|
62
|
self.kernel_manager = kernel_manager
|
|
74
|
self.kernel_manager = kernel_manager
|
|
63
|
self.log = log
|
|
75
|
self.log = log
|
|
|
|
|
76
|
self.kernel_argv = kernel_argv
|
|
64
|
self._routers = {}
|
|
77
|
self._routers = {}
|
|
|
|
|
78
|
self._session_dict = {}
|
|
65
|
|
|
79
|
|
|
66
|
#-------------------------------------------------------------------------
|
|
80
|
#-------------------------------------------------------------------------
|
|
67
|
# Methods for managing kernels and sessions
|
|
81
|
# Methods for managing kernels and sessions
|
|
@@
-72,9
+86,11
b' class NotebookWebApplication(web.Application):'
|
|
72
|
return self.kernel_manager.kernel_ids
|
|
86
|
return self.kernel_manager.kernel_ids
|
|
73
|
|
|
87
|
|
|
74
|
def start_kernel(self):
|
|
88
|
def start_kernel(self):
|
|
75
|
# TODO: pass command line options to the kernel in start_kernel()
|
|
89
|
kwargs = dict()
|
|
76
|
kernel_id = self.kernel_manager.start_kernel()
|
|
90
|
kwargs['extra_arguments'] = self.kernel_argv
|
|
|
|
|
91
|
kernel_id = self.kernel_manager.start_kernel(**kwargs)
|
|
77
|
self.log.info("Kernel started: %s" % kernel_id)
|
|
92
|
self.log.info("Kernel started: %s" % kernel_id)
|
|
|
|
|
93
|
self.log.debug("Kernel args: %r" % kwargs)
|
|
78
|
self.start_session_manager(kernel_id)
|
|
94
|
self.start_session_manager(kernel_id)
|
|
79
|
return kernel_id
|
|
95
|
return kernel_id
|
|
80
|
|
|
96
|
|
|
@@
-87,7
+103,6
b' class NotebookWebApplication(web.Application):'
|
|
87
|
shell_router = ShellStreamRouter(shell_stream)
|
|
103
|
shell_router = ShellStreamRouter(shell_stream)
|
|
88
|
self._routers[(kernel_id, 'iopub')] = iopub_router
|
|
104
|
self._routers[(kernel_id, 'iopub')] = iopub_router
|
|
89
|
self._routers[(kernel_id, 'shell')] = shell_router
|
|
105
|
self._routers[(kernel_id, 'shell')] = shell_router
|
|
90
|
self.log.debug("Session manager started for kernel: %s" % kernel_id)
|
|
|
|
|
91
|
|
|
106
|
|
|
92
|
def kill_kernel(self, kernel_id):
|
|
107
|
def kill_kernel(self, kernel_id):
|
|
93
|
sm = self._session_dict.pop(kernel_id)
|
|
108
|
sm = self._session_dict.pop(kernel_id)
|
|
@@
-139,9
+154,9
b' aliases = dict(ipkernel_aliases)'
|
|
139
|
|
|
154
|
|
|
140
|
aliases.update(dict(
|
|
155
|
aliases.update(dict(
|
|
141
|
ip = 'IPythonNotebookApp.ip',
|
|
156
|
ip = 'IPythonNotebookApp.ip',
|
|
142
|
port = 'IPythonNotebookApp.port'
|
|
157
|
port = 'IPythonNotebookApp.port',
|
|
143
|
colors = 'ZMQInteractiveShell.colors',
|
|
158
|
colors = 'ZMQInteractiveShell.colors',
|
|
144
|
editor = 'IPythonWidget.editor',
|
|
159
|
editor = 'RichIPythonWidget.editor',
|
|
145
|
))
|
|
160
|
))
|
|
146
|
|
|
161
|
|
|
147
|
#-----------------------------------------------------------------------------
|
|
162
|
#-----------------------------------------------------------------------------
|
|
@@
-160,12
+175,17
b' class IPythonNotebookApp(BaseIPythonApplication):'
|
|
160
|
"""
|
|
175
|
"""
|
|
161
|
|
|
176
|
|
|
162
|
classes = [IPKernelApp, ZMQInteractiveShell, ProfileDir, Session,
|
|
177
|
classes = [IPKernelApp, ZMQInteractiveShell, ProfileDir, Session,
|
|
163
|
KernelManager, SessionManager]
|
|
178
|
KernelManager, SessionManager, RichIPythonWidget]
|
|
164
|
flags = Dict(flags)
|
|
179
|
flags = Dict(flags)
|
|
165
|
aliases = Dict(aliases)
|
|
180
|
aliases = Dict(aliases)
|
|
166
|
|
|
181
|
|
|
167
|
kernel_argv = List(Unicode)
|
|
182
|
kernel_argv = List(Unicode)
|
|
168
|
|
|
183
|
|
|
|
|
|
184
|
log_level = Enum((0,10,20,30,40,50,'DEBUG','INFO','WARN','ERROR','CRITICAL'),
|
|
|
|
|
185
|
default_value=logging.INFO,
|
|
|
|
|
186
|
config=True,
|
|
|
|
|
187
|
help="Set the log level by value or name.")
|
|
|
|
|
188
|
|
|
169
|
# connection info:
|
|
189
|
# connection info:
|
|
170
|
ip = Unicode(LOCALHOST, config=True,
|
|
190
|
ip = Unicode(LOCALHOST, config=True,
|
|
171
|
help="The IP address the notebook server will listen on."
|
|
191
|
help="The IP address the notebook server will listen on."
|
|
@@
-185,10
+205,10
b' class IPythonNotebookApp(BaseIPythonApplication):'
|
|
185
|
|
|
205
|
|
|
186
|
self.kernel_argv = list(argv) # copy
|
|
206
|
self.kernel_argv = list(argv) # copy
|
|
187
|
# kernel should inherit default config file from frontend
|
|
207
|
# kernel should inherit default config file from frontend
|
|
188
|
self.kernel_argv.append("KernelApp.parent_appname='%s'"%self.name)
|
|
208
|
self.kernel_argv.append("--KernelApp.parent_appname='%s'"%self.name)
|
|
189
|
# scrub frontend-specific flags
|
|
209
|
# scrub frontend-specific flags
|
|
190
|
for a in argv:
|
|
210
|
for a in argv:
|
|
191
|
if a.startswith('--') and a[2:] in qt_flags:
|
|
211
|
if a.startswith('-') and a.lstrip('-') in notebook_flags:
|
|
192
|
self.kernel_argv.remove(a)
|
|
212
|
self.kernel_argv.remove(a)
|
|
193
|
|
|
213
|
|
|
194
|
def init_kernel_manager(self):
|
|
214
|
def init_kernel_manager(self):
|
|
@@
-198,10
+218,17
b' class IPythonNotebookApp(BaseIPythonApplication):'
|
|
198
|
# Create a KernelManager and start a kernel.
|
|
218
|
# Create a KernelManager and start a kernel.
|
|
199
|
self.kernel_manager = KernelManager(config=self.config, log=self.log)
|
|
219
|
self.kernel_manager = KernelManager(config=self.config, log=self.log)
|
|
200
|
|
|
220
|
|
|
|
|
|
221
|
def init_logging(self):
|
|
|
|
|
222
|
super(IPythonNotebookApp, self).init_logging()
|
|
|
|
|
223
|
# This prevents double log messages because tornado use a root logger that
|
|
|
|
|
224
|
# self.log is a child of. The logging module dipatches log messages to a log
|
|
|
|
|
225
|
# and all of its ancenstors until propagate is set to False.
|
|
|
|
|
226
|
self.log.propagate = False
|
|
|
|
|
227
|
|
|
201
|
def initialize(self, argv=None):
|
|
228
|
def initialize(self, argv=None):
|
|
202
|
super(IPythonNotebookApp, self).initialize(argv)
|
|
229
|
super(IPythonNotebookApp, self).initialize(argv)
|
|
203
|
self.init_kernel_mananger()
|
|
230
|
self.init_kernel_manager()
|
|
204
|
self.web_app = NotebookWebApplication()
|
|
231
|
self.web_app = NotebookWebApplication(self.kernel_manager, self.log, self.kernel_argv)
|
|
205
|
self.http_server = httpserver.HTTPServer(self.web_app)
|
|
232
|
self.http_server = httpserver.HTTPServer(self.web_app)
|
|
206
|
self.http_server.listen(self.port)
|
|
233
|
self.http_server.listen(self.port)
|
|
207
|
|
|
234
|
|