##// END OF EJS Templates
Notebook app debugging....
Brian E. Granger -
Show More
@@ -11,7 +11,7 b' import uuid'
11
11
12 import zmq
12 import zmq
13
13
14 from IPython.config.configurable import Configurable
14 from IPython.config.configurable import LoggingConfigurable
15 from IPython.zmq.ipkernel import launch_kernel
15 from IPython.zmq.ipkernel import launch_kernel
16 from IPython.utils.traitlets import Instance, Dict, Unicode
16 from IPython.utils.traitlets import Instance, Dict, Unicode
17
17
@@ -23,17 +23,13 b' class DuplicateKernelError(Exception):'
23 pass
23 pass
24
24
25
25
26 class KernelManager(Configurable):
26 class KernelManager(LoggingConfigurable):
27 """A class for managing multiple kernels."""
27 """A class for managing multiple kernels."""
28
28
29 context = Instance('zmq.Context')
29 context = Instance('zmq.Context')
30 def _context_default(self):
30 def _context_default(self):
31 return zmq.Context.instance()
31 return zmq.Context.instance()
32
32
33 logname = Unicode('')
34 def _logname_changed(self, name, old, new):
35 self.log = logging.getLogger(new)
36
37 _kernels = Dict()
33 _kernels = Dict()
38
34
39 @property
35 @property
@@ -181,6 +177,6 b' class KernelManager(Configurable):'
181 from sessionmanager import SessionManager
177 from sessionmanager import SessionManager
182 return SessionManager(
178 return SessionManager(
183 kernel_id=kernel_id, kernel_manager=self,
179 kernel_id=kernel_id, kernel_manager=self,
184 config=self.config, context=self.context, logname=self.logname
180 config=self.config, context=self.context, log=self.log
185 )
181 )
186
182
@@ -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
@@ -70,7 +70,7 b' class SessionManager(SessionFactory):'
70 def create_connected_stream(self, port, socket_type):
70 def create_connected_stream(self, port, socket_type):
71 sock = self.context.socket(socket_type)
71 sock = self.context.socket(socket_type)
72 addr = "tcp://%s:%i" % (self.kernel_manager.get_kernel_ip(self.kernel_id), port)
72 addr = "tcp://%s:%i" % (self.kernel_manager.get_kernel_ip(self.kernel_id), port)
73 self.log.info("Connecting to: %s, %r" % (addr, socket_type))
73 self.log.info("Connecting to: %s" % addr)
74 sock.connect(addr)
74 sock.connect(addr)
75 return ZMQStream(sock)
75 return ZMQStream(sock)
76
76
@@ -195,7 +195,7 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):'
195 qtconsole=('IPython.frontend.qt.console.qtconsoleapp.IPythonQtConsoleApp',
195 qtconsole=('IPython.frontend.qt.console.qtconsoleapp.IPythonQtConsoleApp',
196 """Launch the IPython Qt Console."""
196 """Launch the IPython Qt Console."""
197 ),
197 ),
198 hotebook=('IPython.frontend.html.notebook.notebookapp.IPythonNotebookApp',
198 notebook=('IPython.frontend.html.notebook.notebookapp.IPythonNotebookApp',
199 """Launch the IPython HTML Notebook Server"""
199 """Launch the IPython HTML Notebook Server"""
200 ),
200 ),
201 profile = ("IPython.core.profileapp.ProfileApp",
201 profile = ("IPython.core.profileapp.ProfileApp",
General Comments 0
You need to be logged in to leave comments. Login now