##// END OF EJS Templates
Fixing import statments in handlers and notebookapp.
Brian E. Granger -
Show More
@@ -1,275 +1,166 b''
1 import datetime
1 import datetime
2 import json
2 import json
3 import logging
3 import logging
4 import os
4 import os
5 import urllib
5 import urllib
6 import uuid
6 import uuid
7 from Queue import Queue
7 from Queue import Queue
8
8
9 import zmq
10
11 # Install the pyzmq ioloop. This has to be done before anything else from
12 # tornado is imported.
13 from zmq.eventloop import ioloop
14 import tornado.ioloop
15 tornado.ioloop = ioloop
16
17 from tornado import httpserver
18 from tornado import options
9 from tornado import options
19 from tornado import web
10 from tornado import web
20 from tornado import websocket
11 from tornado import websocket
21
12
22 from kernelmanager import KernelManager
23
13
24 options.define("port", default=8888, help="run on the given port", type=int)
14 options.define("port", default=8888, help="run on the given port", type=int)
25
15
26 _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
16 _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
27 _kernel_action_regex = r"(?P<action>restart|interrupt)"
17 _kernel_action_regex = r"(?P<action>restart|interrupt)"
28
18
29 class MainHandler(web.RequestHandler):
19 class MainHandler(web.RequestHandler):
30 def get(self):
20 def get(self):
31 self.render('notebook.html')
21 self.render('notebook.html')
32
22
33
23
34 class KernelHandler(web.RequestHandler):
24 class KernelHandler(web.RequestHandler):
35
25
36 def get(self):
26 def get(self):
37 self.write(json.dumps(self.application.kernel_ids))
27 self.write(json.dumps(self.application.kernel_ids))
38
28
39 def post(self):
29 def post(self):
40 kernel_id = self.application.start_kernel()
30 kernel_id = self.application.start_kernel()
41 self.write(json.dumps(kernel_id))
31 self.write(json.dumps(kernel_id))
42
32
43
33
44 class KernelActionHandler(web.RequestHandler):
34 class KernelActionHandler(web.RequestHandler):
45
35
46 def post(self, kernel_id, action):
36 def post(self, kernel_id, action):
47 # TODO: figure out a better way of handling RPC style calls.
37 # TODO: figure out a better way of handling RPC style calls.
48 if action == 'interrupt':
38 if action == 'interrupt':
49 self.application.interrupt_kernel(kernel_id)
39 self.application.interrupt_kernel(kernel_id)
50 if action == 'restart':
40 if action == 'restart':
51 new_kernel_id = self.application.restart_kernel(kernel_id)
41 new_kernel_id = self.application.restart_kernel(kernel_id)
52 self.write(json.dumps(new_kernel_id))
42 self.write(json.dumps(new_kernel_id))
53
43
54
44
55 class ZMQStreamRouter(object):
45 class ZMQStreamRouter(object):
56
46
57 def __init__(self, zmq_stream):
47 def __init__(self, zmq_stream):
58 self.zmq_stream = zmq_stream
48 self.zmq_stream = zmq_stream
59 self._clients = {}
49 self._clients = {}
60 self.zmq_stream.on_recv(self._on_zmq_reply)
50 self.zmq_stream.on_recv(self._on_zmq_reply)
61
51
62 def register_client(self, client):
52 def register_client(self, client):
63 client_id = uuid.uuid4()
53 client_id = uuid.uuid4()
64 self._clients[client_id] = client
54 self._clients[client_id] = client
65 return client_id
55 return client_id
66
56
67 def unregister_client(self, client_id):
57 def unregister_client(self, client_id):
68 del self._clients[client_id]
58 del self._clients[client_id]
69
59
70 def copy_clients(self, router):
60 def copy_clients(self, router):
71 # Copy the clients of another router.
61 # Copy the clients of another router.
72 for client_id, client in router._clients.items():
62 for client_id, client in router._clients.items():
73 client.router = self
63 client.router = self
74 self._clients[client_id] = client
64 self._clients[client_id] = client
75
65
76
66
77 class IOPubStreamRouter(ZMQStreamRouter):
67 class IOPubStreamRouter(ZMQStreamRouter):
78
68
79 def _on_zmq_reply(self, msg_list):
69 def _on_zmq_reply(self, msg_list):
80 for client_id, client in self._clients.items():
70 for client_id, client in self._clients.items():
81 for msg in msg_list:
71 for msg in msg_list:
82 client.write_message(msg)
72 client.write_message(msg)
83
73
84 def forward_unicode(self, client_id, msg):
74 def forward_unicode(self, client_id, msg):
85 # This is a SUB stream that we should never write to.
75 # This is a SUB stream that we should never write to.
86 pass
76 pass
87
77
88
78
89 class ShellStreamRouter(ZMQStreamRouter):
79 class ShellStreamRouter(ZMQStreamRouter):
90
80
91 def __init__(self, zmq_stream):
81 def __init__(self, zmq_stream):
92 ZMQStreamRouter.__init__(self, zmq_stream)
82 ZMQStreamRouter.__init__(self, zmq_stream)
93 self._request_queue = Queue()
83 self._request_queue = Queue()
94
84
95 def _on_zmq_reply(self, msg_list):
85 def _on_zmq_reply(self, msg_list):
96 client_id = self._request_queue.get(block=False)
86 client_id = self._request_queue.get(block=False)
97 client = self._clients.get(client_id)
87 client = self._clients.get(client_id)
98 if client is not None:
88 if client is not None:
99 for msg in msg_list:
89 for msg in msg_list:
100 client.write_message(msg)
90 client.write_message(msg)
101
91
102 def forward_unicode(self, client_id, msg):
92 def forward_unicode(self, client_id, msg):
103 self._request_queue.put(client_id)
93 self._request_queue.put(client_id)
104 self.zmq_stream.send_unicode(msg)
94 self.zmq_stream.send_unicode(msg)
105
95
106
96
107 class ZMQStreamHandler(websocket.WebSocketHandler):
97 class ZMQStreamHandler(websocket.WebSocketHandler):
108
98
109 def initialize(self, stream_name):
99 def initialize(self, stream_name):
110 self.stream_name = stream_name
100 self.stream_name = stream_name
111
101
112 def open(self, kernel_id):
102 def open(self, kernel_id):
113 self.router = self.application.get_router(kernel_id, self.stream_name)
103 self.router = self.application.get_router(kernel_id, self.stream_name)
114 self.client_id = self.router.register_client(self)
104 self.client_id = self.router.register_client(self)
115 logging.info("Connection open: %s, %s" % (kernel_id, self.client_id))
105 logging.info("Connection open: %s, %s" % (kernel_id, self.client_id))
116
106
117 def on_message(self, msg):
107 def on_message(self, msg):
118 self.router.forward_unicode(self.client_id, msg)
108 self.router.forward_unicode(self.client_id, msg)
119
109
120 def on_close(self):
110 def on_close(self):
121 self.router.unregister_client(self.client_id)
111 self.router.unregister_client(self.client_id)
122 logging.info("Connection closed: %s" % self.client_id)
112 logging.info("Connection closed: %s" % self.client_id)
123
113
124
114
125 class NotebookRootHandler(web.RequestHandler):
115 class NotebookRootHandler(web.RequestHandler):
126
116
127 def get(self):
117 def get(self):
128 files = os.listdir(os.getcwd())
118 files = os.listdir(os.getcwd())
129 files = [file for file in files if file.endswith(".ipynb")]
119 files = [file for file in files if file.endswith(".ipynb")]
130 self.write(json.dumps(files))
120 self.write(json.dumps(files))
131
121
132
122
133 class NotebookHandler(web.RequestHandler):
123 class NotebookHandler(web.RequestHandler):
134
124
135 SUPPORTED_METHODS = ("GET", "DELETE", "PUT")
125 SUPPORTED_METHODS = ("GET", "DELETE", "PUT")
136
126
137 def find_path(self, filename):
127 def find_path(self, filename):
138 filename = urllib.unquote(filename)
128 filename = urllib.unquote(filename)
139 if not filename.endswith('.ipynb'):
129 if not filename.endswith('.ipynb'):
140 raise web.HTTPError(400)
130 raise web.HTTPError(400)
141 path = os.path.join(os.getcwd(), filename)
131 path = os.path.join(os.getcwd(), filename)
142 return path
132 return path
143
133
144 def get(self, filename):
134 def get(self, filename):
145 path = self.find_path(filename)
135 path = self.find_path(filename)
146 if not os.path.isfile(path):
136 if not os.path.isfile(path):
147 raise web.HTTPError(404)
137 raise web.HTTPError(404)
148 info = os.stat(path)
138 info = os.stat(path)
149 self.set_header("Content-Type", "application/unknown")
139 self.set_header("Content-Type", "application/unknown")
150 self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp(
140 self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp(
151 info.st_mtime))
141 info.st_mtime))
152 f = open(path, "r")
142 f = open(path, "r")
153 try:
143 try:
154 self.finish(f.read())
144 self.finish(f.read())
155 finally:
145 finally:
156 f.close()
146 f.close()
157
147
158 def put(self, filename):
148 def put(self, filename):
159 path = self.find_path(filename)
149 path = self.find_path(filename)
160 f = open(path, "w")
150 f = open(path, "w")
161 f.write(self.request.body)
151 f.write(self.request.body)
162 f.close()
152 f.close()
163 self.finish()
153 self.finish()
164
154
165 def delete(self, filename):
155 def delete(self, filename):
166 path = self.find_path(filename)
156 path = self.find_path(filename)
167 if not os.path.isfile(path):
157 if not os.path.isfile(path):
168 raise web.HTTPError(404)
158 raise web.HTTPError(404)
169 os.unlink(path)
159 os.unlink(path)
170 self.set_status(204)
160 self.set_status(204)
171 self.finish()
161 self.finish()
172
162
173
163
174 class NotebookApplication(web.Application):
164
175
165
176 def __init__(self):
177 handlers = [
178 (r"/", MainHandler),
179 (r"/kernels", KernelHandler),
180 (r"/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler),
181 (r"/kernels/%s/iopub" % _kernel_id_regex, ZMQStreamHandler, dict(stream_name='iopub')),
182 (r"/kernels/%s/shell" % _kernel_id_regex, ZMQStreamHandler, dict(stream_name='shell')),
183 (r"/notebooks", NotebookRootHandler),
184 (r"/notebooks/([^/]+)", NotebookHandler)
185 ]
186 settings = dict(
187 template_path=os.path.join(os.path.dirname(__file__), "templates"),
188 static_path=os.path.join(os.path.dirname(__file__), "static"),
189 )
190 web.Application.__init__(self, handlers, **settings)
191
192 self.context = zmq.Context()
193 self.kernel_manager = KernelManager(self.context)
194 self._session_dict = {}
195 self._routers = {}
196
197 #-------------------------------------------------------------------------
198 # Methods for managing kernels and sessions
199 #-------------------------------------------------------------------------
200
201 @property
202 def kernel_ids(self):
203 return self.kernel_manager.kernel_ids
204
205 def start_kernel(self):
206 kernel_id = self.kernel_manager.start_kernel()
207 logging.info("Kernel started: %s" % kernel_id)
208 self.start_session(kernel_id)
209 return kernel_id
210
211 def interrupt_kernel(self, kernel_id):
212 self.kernel_manager.interrupt_kernel(kernel_id)
213 logging.info("Kernel interrupted: %s" % kernel_id)
214
215 def restart_kernel(self, kernel_id):
216 # Create the new kernel first so we can move the clients over.
217 new_kernel_id = self.start_kernel()
218
219 # Copy the clients over to the new routers.
220 old_iopub_router = self.get_router(kernel_id, 'iopub')
221 old_shell_router = self.get_router(kernel_id, 'shell')
222 new_iopub_router = self.get_router(new_kernel_id, 'iopub')
223 new_shell_router = self.get_router(new_kernel_id, 'shell')
224 new_iopub_router.copy_clients(old_iopub_router)
225 new_shell_router.copy_clients(old_shell_router)
226
227 # Now shutdown the old session and the kernel.
228 # TODO: This causes a hard crash in ZMQStream.close, which sets
229 # self.socket to None to hastily. We will need to fix this in PyZMQ
230 # itself. For now, we just leave the old kernel running :(
231 # sm = self.kernel_manager.get_session_manager(kernel_id)
232 # session_id = self._session_dict[kernel_id]
233 # sm.stop_session(session_id)
234 # self.kernel_manager.kill_kernel(kernel_id)
235
236 logging.info("Kernel restarted")
237 return new_kernel_id
238
239 def start_session(self, kernel_id):
240 sm = self.kernel_manager.get_session_manager(kernel_id)
241 session_id = sm.start_session()
242 self._session_dict[kernel_id] = session_id
243 iopub_stream = sm.get_iopub_stream(session_id)
244 shell_stream = sm.get_shell_stream(session_id)
245 iopub_router = IOPubStreamRouter(iopub_stream)
246 shell_router = ShellStreamRouter(shell_stream)
247 self._routers[(kernel_id, session_id, 'iopub')] = iopub_router
248 self._routers[(kernel_id, session_id, 'shell')] = shell_router
249 logging.info("Session started: %s, %s" % (kernel_id, session_id))
250
251 def stop_session(self, kernel_id):
252 # TODO: finish this!
253 sm = self.kernel_manager.get_session_manager(kernel_id)
254 session_id = self._session_dict[kernel_id]
255
256 def get_router(self, kernel_id, stream_name):
257 session_id = self._session_dict[kernel_id]
258 router = self._routers[(kernel_id, session_id, stream_name)]
259 return router
260
261
262 def main():
263 options.parse_command_line()
264 application = NotebookApplication()
265 http_server = httpserver.HTTPServer(application)
266 http_server.listen(options.options.port)
267 print "IPython Notebook running at: http://127.0.0.1:8888"
268 print "The github master of tornado is required to run this server:"
269 print " https://github.com/facebook/tornado/tree/master/tornado"
270 ioloop.IOLoop.instance().start()
271
272
273 if __name__ == "__main__":
274 main()
275
166
@@ -1,275 +1,131 b''
1 import datetime
2 import json
3 import logging
1 import logging
4 import os
2 import os
5 import urllib
6 import uuid
7 from Queue import Queue
8
3
9 import zmq
4 import zmq
10
5
11 # Install the pyzmq ioloop. This has to be done before anything else from
6 # Install the pyzmq ioloop. This has to be done before anything else from
12 # tornado is imported.
7 # tornado is imported.
13 from zmq.eventloop import ioloop
8 from zmq.eventloop import ioloop
14 import tornado.ioloop
9 import tornado.ioloop
15 tornado.ioloop = ioloop
10 tornado.ioloop = ioloop
16
11
17 from tornado import httpserver
12 from tornado import httpserver
18 from tornado import options
13 from tornado import options
19 from tornado import web
14 from tornado import web
20 from tornado import websocket
21
15
22 from kernelmanager import KernelManager
16 from kernelmanager import KernelManager
17 from handlers import (
18 MainHandler, KernelHandler, KernelActionHandler, ZMQStreamHandler,
19 NotebookRootHandler, NotebookHandler
20 )
21 from routers import IOPubStreamRouter, ShellStreamRouter
23
22
24 options.define("port", default=8888, help="run on the given port", type=int)
23 options.define("port", default=8888, help="run on the given port", type=int)
25
24
26 _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
25 _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
27 _kernel_action_regex = r"(?P<action>restart|interrupt)"
26 _kernel_action_regex = r"(?P<action>restart|interrupt)"
28
27
29 class MainHandler(web.RequestHandler):
30 def get(self):
31 self.render('notebook.html')
32
33
34 class KernelHandler(web.RequestHandler):
35
36 def get(self):
37 self.write(json.dumps(self.application.kernel_ids))
38
39 def post(self):
40 kernel_id = self.application.start_kernel()
41 self.write(json.dumps(kernel_id))
42
43
44 class KernelActionHandler(web.RequestHandler):
45
46 def post(self, kernel_id, action):
47 # TODO: figure out a better way of handling RPC style calls.
48 if action == 'interrupt':
49 self.application.interrupt_kernel(kernel_id)
50 if action == 'restart':
51 new_kernel_id = self.application.restart_kernel(kernel_id)
52 self.write(json.dumps(new_kernel_id))
53
54
55 class ZMQStreamRouter(object):
56
57 def __init__(self, zmq_stream):
58 self.zmq_stream = zmq_stream
59 self._clients = {}
60 self.zmq_stream.on_recv(self._on_zmq_reply)
61
62 def register_client(self, client):
63 client_id = uuid.uuid4()
64 self._clients[client_id] = client
65 return client_id
66
67 def unregister_client(self, client_id):
68 del self._clients[client_id]
69
70 def copy_clients(self, router):
71 # Copy the clients of another router.
72 for client_id, client in router._clients.items():
73 client.router = self
74 self._clients[client_id] = client
75
76
77 class IOPubStreamRouter(ZMQStreamRouter):
78
79 def _on_zmq_reply(self, msg_list):
80 for client_id, client in self._clients.items():
81 for msg in msg_list:
82 client.write_message(msg)
83
84 def forward_unicode(self, client_id, msg):
85 # This is a SUB stream that we should never write to.
86 pass
87
88
89 class ShellStreamRouter(ZMQStreamRouter):
90
91 def __init__(self, zmq_stream):
92 ZMQStreamRouter.__init__(self, zmq_stream)
93 self._request_queue = Queue()
94
95 def _on_zmq_reply(self, msg_list):
96 client_id = self._request_queue.get(block=False)
97 client = self._clients.get(client_id)
98 if client is not None:
99 for msg in msg_list:
100 client.write_message(msg)
101
102 def forward_unicode(self, client_id, msg):
103 self._request_queue.put(client_id)
104 self.zmq_stream.send_unicode(msg)
105
106
107 class ZMQStreamHandler(websocket.WebSocketHandler):
108
109 def initialize(self, stream_name):
110 self.stream_name = stream_name
111
112 def open(self, kernel_id):
113 self.router = self.application.get_router(kernel_id, self.stream_name)
114 self.client_id = self.router.register_client(self)
115 logging.info("Connection open: %s, %s" % (kernel_id, self.client_id))
116
117 def on_message(self, msg):
118 self.router.forward_unicode(self.client_id, msg)
119
120 def on_close(self):
121 self.router.unregister_client(self.client_id)
122 logging.info("Connection closed: %s" % self.client_id)
123
124
125 class NotebookRootHandler(web.RequestHandler):
126
127 def get(self):
128 files = os.listdir(os.getcwd())
129 files = [file for file in files if file.endswith(".ipynb")]
130 self.write(json.dumps(files))
131
132
133 class NotebookHandler(web.RequestHandler):
134
135 SUPPORTED_METHODS = ("GET", "DELETE", "PUT")
136
137 def find_path(self, filename):
138 filename = urllib.unquote(filename)
139 if not filename.endswith('.ipynb'):
140 raise web.HTTPError(400)
141 path = os.path.join(os.getcwd(), filename)
142 return path
143
144 def get(self, filename):
145 path = self.find_path(filename)
146 if not os.path.isfile(path):
147 raise web.HTTPError(404)
148 info = os.stat(path)
149 self.set_header("Content-Type", "application/unknown")
150 self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp(
151 info.st_mtime))
152 f = open(path, "r")
153 try:
154 self.finish(f.read())
155 finally:
156 f.close()
157
158 def put(self, filename):
159 path = self.find_path(filename)
160 f = open(path, "w")
161 f.write(self.request.body)
162 f.close()
163 self.finish()
164
165 def delete(self, filename):
166 path = self.find_path(filename)
167 if not os.path.isfile(path):
168 raise web.HTTPError(404)
169 os.unlink(path)
170 self.set_status(204)
171 self.finish()
172
28
173
29
174 class NotebookApplication(web.Application):
30 class NotebookApplication(web.Application):
175
31
176 def __init__(self):
32 def __init__(self):
177 handlers = [
33 handlers = [
178 (r"/", MainHandler),
34 (r"/", MainHandler),
179 (r"/kernels", KernelHandler),
35 (r"/kernels", KernelHandler),
180 (r"/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler),
36 (r"/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler),
181 (r"/kernels/%s/iopub" % _kernel_id_regex, ZMQStreamHandler, dict(stream_name='iopub')),
37 (r"/kernels/%s/iopub" % _kernel_id_regex, ZMQStreamHandler, dict(stream_name='iopub')),
182 (r"/kernels/%s/shell" % _kernel_id_regex, ZMQStreamHandler, dict(stream_name='shell')),
38 (r"/kernels/%s/shell" % _kernel_id_regex, ZMQStreamHandler, dict(stream_name='shell')),
183 (r"/notebooks", NotebookRootHandler),
39 (r"/notebooks", NotebookRootHandler),
184 (r"/notebooks/([^/]+)", NotebookHandler)
40 (r"/notebooks/([^/]+)", NotebookHandler)
185 ]
41 ]
186 settings = dict(
42 settings = dict(
187 template_path=os.path.join(os.path.dirname(__file__), "templates"),
43 template_path=os.path.join(os.path.dirname(__file__), "templates"),
188 static_path=os.path.join(os.path.dirname(__file__), "static"),
44 static_path=os.path.join(os.path.dirname(__file__), "static"),
189 )
45 )
190 web.Application.__init__(self, handlers, **settings)
46 web.Application.__init__(self, handlers, **settings)
191
47
192 self.context = zmq.Context()
48 self.context = zmq.Context()
193 self.kernel_manager = KernelManager(self.context)
49 self.kernel_manager = KernelManager(self.context)
194 self._session_dict = {}
50 self._session_dict = {}
195 self._routers = {}
51 self._routers = {}
196
52
197 #-------------------------------------------------------------------------
53 #-------------------------------------------------------------------------
198 # Methods for managing kernels and sessions
54 # Methods for managing kernels and sessions
199 #-------------------------------------------------------------------------
55 #-------------------------------------------------------------------------
200
56
201 @property
57 @property
202 def kernel_ids(self):
58 def kernel_ids(self):
203 return self.kernel_manager.kernel_ids
59 return self.kernel_manager.kernel_ids
204
60
205 def start_kernel(self):
61 def start_kernel(self):
206 kernel_id = self.kernel_manager.start_kernel()
62 kernel_id = self.kernel_manager.start_kernel()
207 logging.info("Kernel started: %s" % kernel_id)
63 logging.info("Kernel started: %s" % kernel_id)
208 self.start_session(kernel_id)
64 self.start_session(kernel_id)
209 return kernel_id
65 return kernel_id
210
66
211 def interrupt_kernel(self, kernel_id):
67 def interrupt_kernel(self, kernel_id):
212 self.kernel_manager.interrupt_kernel(kernel_id)
68 self.kernel_manager.interrupt_kernel(kernel_id)
213 logging.info("Kernel interrupted: %s" % kernel_id)
69 logging.info("Kernel interrupted: %s" % kernel_id)
214
70
215 def restart_kernel(self, kernel_id):
71 def restart_kernel(self, kernel_id):
216 # Create the new kernel first so we can move the clients over.
72 # Create the new kernel first so we can move the clients over.
217 new_kernel_id = self.start_kernel()
73 new_kernel_id = self.start_kernel()
218
74
219 # Copy the clients over to the new routers.
75 # Copy the clients over to the new routers.
220 old_iopub_router = self.get_router(kernel_id, 'iopub')
76 old_iopub_router = self.get_router(kernel_id, 'iopub')
221 old_shell_router = self.get_router(kernel_id, 'shell')
77 old_shell_router = self.get_router(kernel_id, 'shell')
222 new_iopub_router = self.get_router(new_kernel_id, 'iopub')
78 new_iopub_router = self.get_router(new_kernel_id, 'iopub')
223 new_shell_router = self.get_router(new_kernel_id, 'shell')
79 new_shell_router = self.get_router(new_kernel_id, 'shell')
224 new_iopub_router.copy_clients(old_iopub_router)
80 new_iopub_router.copy_clients(old_iopub_router)
225 new_shell_router.copy_clients(old_shell_router)
81 new_shell_router.copy_clients(old_shell_router)
226
82
227 # Now shutdown the old session and the kernel.
83 # Now shutdown the old session and the kernel.
228 # TODO: This causes a hard crash in ZMQStream.close, which sets
84 # TODO: This causes a hard crash in ZMQStream.close, which sets
229 # self.socket to None to hastily. We will need to fix this in PyZMQ
85 # self.socket to None to hastily. We will need to fix this in PyZMQ
230 # itself. For now, we just leave the old kernel running :(
86 # itself. For now, we just leave the old kernel running :(
231 # sm = self.kernel_manager.get_session_manager(kernel_id)
87 # sm = self.kernel_manager.get_session_manager(kernel_id)
232 # session_id = self._session_dict[kernel_id]
88 # session_id = self._session_dict[kernel_id]
233 # sm.stop_session(session_id)
89 # sm.stop_session(session_id)
234 # self.kernel_manager.kill_kernel(kernel_id)
90 # self.kernel_manager.kill_kernel(kernel_id)
235
91
236 logging.info("Kernel restarted")
92 logging.info("Kernel restarted")
237 return new_kernel_id
93 return new_kernel_id
238
94
239 def start_session(self, kernel_id):
95 def start_session(self, kernel_id):
240 sm = self.kernel_manager.get_session_manager(kernel_id)
96 sm = self.kernel_manager.get_session_manager(kernel_id)
241 session_id = sm.start_session()
97 session_id = sm.start_session()
242 self._session_dict[kernel_id] = session_id
98 self._session_dict[kernel_id] = session_id
243 iopub_stream = sm.get_iopub_stream(session_id)
99 iopub_stream = sm.get_iopub_stream(session_id)
244 shell_stream = sm.get_shell_stream(session_id)
100 shell_stream = sm.get_shell_stream(session_id)
245 iopub_router = IOPubStreamRouter(iopub_stream)
101 iopub_router = IOPubStreamRouter(iopub_stream)
246 shell_router = ShellStreamRouter(shell_stream)
102 shell_router = ShellStreamRouter(shell_stream)
247 self._routers[(kernel_id, session_id, 'iopub')] = iopub_router
103 self._routers[(kernel_id, session_id, 'iopub')] = iopub_router
248 self._routers[(kernel_id, session_id, 'shell')] = shell_router
104 self._routers[(kernel_id, session_id, 'shell')] = shell_router
249 logging.info("Session started: %s, %s" % (kernel_id, session_id))
105 logging.info("Session started: %s, %s" % (kernel_id, session_id))
250
106
251 def stop_session(self, kernel_id):
107 def stop_session(self, kernel_id):
252 # TODO: finish this!
108 # TODO: finish this!
253 sm = self.kernel_manager.get_session_manager(kernel_id)
109 sm = self.kernel_manager.get_session_manager(kernel_id)
254 session_id = self._session_dict[kernel_id]
110 session_id = self._session_dict[kernel_id]
255
111
256 def get_router(self, kernel_id, stream_name):
112 def get_router(self, kernel_id, stream_name):
257 session_id = self._session_dict[kernel_id]
113 session_id = self._session_dict[kernel_id]
258 router = self._routers[(kernel_id, session_id, stream_name)]
114 router = self._routers[(kernel_id, session_id, stream_name)]
259 return router
115 return router
260
116
261
117
262 def main():
118 def main():
263 options.parse_command_line()
119 options.parse_command_line()
264 application = NotebookApplication()
120 application = NotebookApplication()
265 http_server = httpserver.HTTPServer(application)
121 http_server = httpserver.HTTPServer(application)
266 http_server.listen(options.options.port)
122 http_server.listen(options.options.port)
267 print "IPython Notebook running at: http://127.0.0.1:8888"
123 print "IPython Notebook running at: http://127.0.0.1:8888"
268 print "The github master of tornado is required to run this server:"
124 print "The github master of tornado is required to run this server:"
269 print " https://github.com/facebook/tornado/tree/master/tornado"
125 print " https://github.com/facebook/tornado/tree/master/tornado"
270 ioloop.IOLoop.instance().start()
126 ioloop.IOLoop.instance().start()
271
127
272
128
273 if __name__ == "__main__":
129 if __name__ == "__main__":
274 main()
130 main()
275
131
General Comments 0
You need to be logged in to leave comments. Login now