##// END OF EJS Templates
Basic server for htmlnotebook working.
Brian Granger -
Show More
@@ -1,11 +1,14 b''
1 import signal
1 import signal
2 import sys
2 import sys
3 import uuid
4
3
5 from IPython.zmq.ipkernel import launch_kernel
4 from IPython.zmq.ipkernel import launch_kernel
6 from session import SessionManager
5 from session import SessionManager
7
6
8
7
8 class DuplicateKernelError(Exception):
9 pass
10
11
9 class KernelManager(object):
12 class KernelManager(object):
10
13
11 ip = '127.0.0.1'
14 ip = '127.0.0.1'
@@ -27,8 +30,9 b' class KernelManager(object):'
27 else:
30 else:
28 return False
31 return False
29
32
30 def start_kernel(self):
33 def start_kernel(self, kernel_id):
31 kid = str(uuid.uuid4())
34 if kernel_id in self._kernels:
35 raise DuplicateKernelError("Kernel already exists: %s" % kernel_id)
32 (process, shell_port, iopub_port, stdin_port, hb_port) = launch_kernel()
36 (process, shell_port, iopub_port, stdin_port, hb_port) = launch_kernel()
33 d = dict(
37 d = dict(
34 process = process,
38 process = process,
@@ -36,10 +40,10 b' class KernelManager(object):'
36 iopub_port = iopub_port,
40 iopub_port = iopub_port,
37 shell_port = shell_port,
41 shell_port = shell_port,
38 hb_port = hb_port,
42 hb_port = hb_port,
39 session_manager = SessionManager(self, kid, self.context)
43 session_manager = SessionManager(self, kernel_id, self.context)
40 )
44 )
41 self._kernels[kid] = d
45 self._kernels[kernel_id] = d
42 return kid
46 return kernel_id
43
47
44 def kill_kernel(self, kernel_id):
48 def kill_kernel(self, kernel_id):
45 kernel_process = self.get_kernel_process(kernel_id)
49 kernel_process = self.get_kernel_process(kernel_id)
@@ -1,13 +1,11 b''
1 import json
1 import json
2 import logging
2 import logging
3 import os
3 import os
4 import uuid
5
4
6 import zmq
5 import zmq
7
6
8 # Install the pyzmq ioloop. This has to be done before anything else from
7 # Install the pyzmq ioloop. This has to be done before anything else from
9 # tornado is imported.
8 # tornado is imported.
10 from zmq.eventloop.zmqstream import ZMQStream
11 from zmq.eventloop import ioloop
9 from zmq.eventloop import ioloop
12 import tornado.ioloop
10 import tornado.ioloop
13 tornado.ioloop = ioloop
11 tornado.ioloop = ioloop
@@ -21,8 +19,8 b' from kernelmanager import KernelManager'
21
19
22 options.define("port", default=8888, help="run on the given port", type=int)
20 options.define("port", default=8888, help="run on the given port", type=int)
23
21
24 _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
22 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
25 _session_id_regex = r"(?P<session_id>\w+)"
23 _kernel_id_regex = r"(?P<kernel_id>\w+)"
26
24
27
25
28 class MainHandler(web.RequestHandler):
26 class MainHandler(web.RequestHandler):
@@ -30,30 +28,44 b' class MainHandler(web.RequestHandler):'
30 self.render('notebook.html')
28 self.render('notebook.html')
31
29
32
30
33 class KernelHandler(web.RequestHandler):
31 class BaseKernelHandler(object):
32
33 def get_kernel(self):
34 return self.application.kernel_manager
35
36 def get_session(self, kernel_id):
37 km = self.get_kernel()
38 sm = km.get_session_manager(kernel_id)
39 return sm
40
41
42 class KernelHandler(web.RequestHandler, BaseKernelHandler):
34
43
35 def get(self):
44 def get(self):
36 self.write(json.dumps(self.application.kernel_manager.kernel_ids))
45 self.write(json.dumps(self.get_kernel().kernel_ids))
46
47 def post(self, *args, **kwargs):
48 kernel_id = kwargs['kernel_id']
49 self.get_kernel().start_kernel(kernel_id)
50 logging.info("Starting kernel: %s" % kernel_id)
51 self.write(json.dumps(kernel_id))
37
52
38 def post(self):
39 kid = self.application.kernel_manager.start_kernel()
40 logging.info("Starting kernel: %s" % kid)
41 self.write(json.dumps(kid))
42
53
54 class SessionHandler(web.RequestHandler, BaseKernelHandler):
43
55
44 class SessionHandler(web.RequestHandler):
56 def get(self, *args, **kwargs):
57 kernel_id = kwargs['kernel_id']
58 self.write(json.dumps(self.get_session(kernel_id).session_ids))
45
59
46 def post(self, *args, **kwargs):
60 def post(self, *args, **kwargs):
47 kernel_id = kwargs['kernel_id']
61 kernel_id = kwargs['kernel_id']
48 session_id = kwargs['session_id']
62 sm = self.get_session(kernel_id)
63 session_id = sm.start_session()
49 logging.info("Starting session: %s, %s" % (kernel_id,session_id))
64 logging.info("Starting session: %s, %s" % (kernel_id, session_id))
50 km = self.application.kernel_manager
65 self.write(json.dumps(session_id))
51 sm = km.get_session_manager(kernel_id)
52 sm.start_session(session_id)
53 self.finish()
54
66
55
67
56 class ZMQStreamHandler(websocket.WebSocketHandler):
68 class ZMQStreamHandler(websocket.WebSocketHandler, BaseKernelHandler):
57
69
58 stream_name = ''
70 stream_name = ''
59
71
@@ -61,20 +73,17 b' class ZMQStreamHandler(websocket.WebSocketHandler):'
61 kernel_id = kwargs['kernel_id']
73 kernel_id = kwargs['kernel_id']
62 session_id = kwargs['session_id']
74 session_id = kwargs['session_id']
63 logging.info("Connection open: %s, %s" % (kernel_id,session_id))
75 logging.info("Connection open: %s, %s" % (kernel_id,session_id))
64 sm = self.application.kernel_manager.get_session_manager(kernel_id)
76 sm = self.get_session(kernel_id)
65 method_name = "get_%s_stream" % self.stream_name
77 method_name = "get_%s_stream" % self.stream_name
66 method = getattr(sm, method_name)
78 method = getattr(sm, method_name)
67 self.zmq_stream = method(session_id)
79 self.zmq_stream = method(session_id)
68 self.zmq_stream.on_recv(self._on_zmq_reply)
80 self.zmq_stream.on_recv(self._on_zmq_reply)
69 self.session_manager = sm
70 self.session_id = session_id
71
81
72 def on_message(self, msg):
82 def on_message(self, msg):
73 logging.info("Message received: %r" % msg)
83 logging.info("Message received: %r" % msg)
74 self.zmq_stream.send(msg)
84 self.zmq_stream.send_unicode(msg)
75
85
76 def on_close(self):
86 def on_close(self):
77 logging.info("Connection closed: %s, %s" % (kernel_id,session_id))
78 self.zmq_stream.close()
87 self.zmq_stream.close()
79
88
80 def _on_zmq_reply(self, msg):
89 def _on_zmq_reply(self, msg):
@@ -97,8 +106,8 b' class NotebookApplication(web.Application):'
97 def __init__(self):
106 def __init__(self):
98 handlers = [
107 handlers = [
99 (r"/", MainHandler),
108 (r"/", MainHandler),
100 (r"/kernels", KernelHandler),
109 (r"/kernels/%s" % (_kernel_id_regex,), KernelHandler),
101 (r"/kernels/%s/sessions/%s" % (_kernel_id_regex,_session_id_regex), SessionHandler),
110 (r"/kernels/%s/sessions" % (_kernel_id_regex,), SessionHandler),
102 (r"/kernels/%s/sessions/%s/iopub" % (_kernel_id_regex,_session_id_regex), IOPubStreamHandler),
111 (r"/kernels/%s/sessions/%s/iopub" % (_kernel_id_regex,_session_id_regex), IOPubStreamHandler),
103 (r"/kernels/%s/sessions/%s/shell" % (_kernel_id_regex,_session_id_regex), ShellStreamHandler),
112 (r"/kernels/%s/sessions/%s/shell" % (_kernel_id_regex,_session_id_regex), ShellStreamHandler),
104 ]
113 ]
@@ -1,4 +1,5 b''
1 import logging
1 import logging
2 import uuid
2
3
3 import zmq
4 import zmq
4 from zmq.eventloop.zmqstream import ZMQStream
5 from zmq.eventloop.zmqstream import ZMQStream
@@ -15,7 +16,21 b' class SessionManager(object):'
15 def __del__(self):
16 def __del__(self):
16 self.stop_all()
17 self.stop_all()
17
18
18 def start_session(self, session_id):
19 @property
20 def session_ids(self):
21 return self._session.keys()
22
23 def __len__(self):
24 return len(self.session_ids)
25
26 def __contains__(self, session_id):
27 if session_id in self.session_ids:
28 return True
29 else:
30 return False
31
32 def start_session(self):
33 session_id = str(uuid.uuid4())
19 ports = self.kernel_manager.get_kernel_ports(self.kernel_id)
34 ports = self.kernel_manager.get_kernel_ports(self.kernel_id)
20 iopub_stream = self.create_connected_stream(ports['iopub_port'], zmq.SUB)
35 iopub_stream = self.create_connected_stream(ports['iopub_port'], zmq.SUB)
21 shell_stream = self.create_connected_stream(ports['shell_port'], zmq.XREQ)
36 shell_stream = self.create_connected_stream(ports['shell_port'], zmq.XREQ)
@@ -23,6 +38,7 b' class SessionManager(object):'
23 iopub_stream = iopub_stream,
38 iopub_stream = iopub_stream,
24 shell_stream = shell_stream
39 shell_stream = shell_stream
25 )
40 )
41 return session_id
26
42
27 def stop_session(self, session_id):
43 def stop_session(self, session_id):
28 session_dict = self._sessions.get(session_id)
44 session_dict = self._sessions.get(session_id)
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now