Show More
@@ -1,186 +1,187 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 |
|
6 | |||
7 | import zmq |
|
7 | import zmq | |
8 |
|
8 | |||
9 | # Install the pyzmq ioloop. This has to be done before anything else from |
|
9 | # Install the pyzmq ioloop. This has to be done before anything else from | |
10 | # tornado is imported. |
|
10 | # tornado is imported. | |
11 | from zmq.eventloop import ioloop |
|
11 | from zmq.eventloop import ioloop | |
12 | import tornado.ioloop |
|
12 | import tornado.ioloop | |
13 | tornado.ioloop = ioloop |
|
13 | tornado.ioloop = ioloop | |
14 |
|
14 | |||
15 | from tornado import httpserver |
|
15 | from tornado import httpserver | |
16 | from tornado import options |
|
16 | from tornado import options | |
17 | from tornado import web |
|
17 | from tornado import web | |
18 | from tornado import websocket |
|
18 | from tornado import websocket | |
19 |
|
19 | |||
20 | from kernelmanager import KernelManager |
|
20 | from kernelmanager import KernelManager | |
21 |
|
21 | |||
22 | options.define("port", default=8888, help="run on the given port", type=int) |
|
22 | options.define("port", default=8888, help="run on the given port", type=int) | |
23 |
|
23 | |||
24 | _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" |
|
24 | _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" | |
25 | _kernel_id_regex = r"(?P<kernel_id>\w+)" |
|
25 | _kernel_id_regex = r"(?P<kernel_id>\w+)" | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class MainHandler(web.RequestHandler): |
|
28 | class MainHandler(web.RequestHandler): | |
29 | def get(self): |
|
29 | def get(self): | |
30 | self.render('notebook.html') |
|
30 | self.render('notebook.html') | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class BaseKernelHandler(object): |
|
33 | class BaseKernelHandler(object): | |
34 |
|
34 | |||
35 | def get_kernel(self): |
|
35 | def get_kernel(self): | |
36 | return self.application.kernel_manager |
|
36 | return self.application.kernel_manager | |
37 |
|
37 | |||
38 | def get_session(self, kernel_id): |
|
38 | def get_session(self, kernel_id): | |
39 | km = self.get_kernel() |
|
39 | km = self.get_kernel() | |
40 | sm = km.get_session_manager(kernel_id) |
|
40 | sm = km.get_session_manager(kernel_id) | |
41 | return sm |
|
41 | return sm | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | class KernelHandler(web.RequestHandler, BaseKernelHandler): |
|
44 | class KernelHandler(web.RequestHandler, BaseKernelHandler): | |
45 |
|
45 | |||
46 | def get(self): |
|
46 | def get(self): | |
47 | self.write(json.dumps(self.get_kernel().kernel_ids)) |
|
47 | self.write(json.dumps(self.get_kernel().kernel_ids)) | |
48 |
|
48 | |||
49 | def post(self, *args, **kwargs): |
|
49 | def post(self, *args, **kwargs): | |
50 | kernel_id = kwargs['kernel_id'] |
|
50 | kernel_id = kwargs['kernel_id'] | |
51 | self.get_kernel().start_kernel(kernel_id) |
|
51 | self.get_kernel().start_kernel(kernel_id) | |
52 | logging.info("Starting kernel: %s" % kernel_id) |
|
52 | logging.info("Starting kernel: %s" % kernel_id) | |
53 | self.write(json.dumps(kernel_id)) |
|
53 | self.write(json.dumps(kernel_id)) | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | class SessionHandler(web.RequestHandler, BaseKernelHandler): |
|
56 | class SessionHandler(web.RequestHandler, BaseKernelHandler): | |
57 |
|
57 | |||
58 | def get(self, *args, **kwargs): |
|
58 | def get(self, *args, **kwargs): | |
59 | kernel_id = kwargs['kernel_id'] |
|
59 | kernel_id = kwargs['kernel_id'] | |
60 | self.write(json.dumps(self.get_session(kernel_id).session_ids)) |
|
60 | self.write(json.dumps(self.get_session(kernel_id).session_ids)) | |
61 |
|
61 | |||
62 | def post(self, *args, **kwargs): |
|
62 | def post(self, *args, **kwargs): | |
63 | kernel_id = kwargs['kernel_id'] |
|
63 | kernel_id = kwargs['kernel_id'] | |
64 | sm = self.get_session(kernel_id) |
|
64 | sm = self.get_session(kernel_id) | |
65 | session_id = sm.start_session() |
|
65 | session_id = sm.start_session() | |
66 | logging.info("Starting session: %s, %s" % (kernel_id, session_id)) |
|
66 | logging.info("Starting session: %s, %s" % (kernel_id, session_id)) | |
67 | self.write(json.dumps(session_id)) |
|
67 | self.write(json.dumps(session_id)) | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | class ZMQStreamHandler(websocket.WebSocketHandler, BaseKernelHandler): |
|
70 | class ZMQStreamHandler(websocket.WebSocketHandler, BaseKernelHandler): | |
71 |
|
71 | |||
72 | stream_name = '' |
|
72 | stream_name = '' | |
73 |
|
73 | |||
74 | def open(self, *args, **kwargs): |
|
74 | def open(self, *args, **kwargs): | |
75 | kernel_id = kwargs['kernel_id'] |
|
75 | kernel_id = kwargs['kernel_id'] | |
76 | session_id = kwargs['session_id'] |
|
76 | session_id = kwargs['session_id'] | |
77 | logging.info("Connection open: %s, %s" % (kernel_id,session_id)) |
|
77 | logging.info("Connection open: %s, %s" % (kernel_id,session_id)) | |
78 | sm = self.get_session(kernel_id) |
|
78 | sm = self.get_session(kernel_id) | |
79 | method_name = "get_%s_stream" % self.stream_name |
|
79 | method_name = "get_%s_stream" % self.stream_name | |
80 | method = getattr(sm, method_name) |
|
80 | method = getattr(sm, method_name) | |
81 | self.zmq_stream = method(session_id) |
|
81 | self.zmq_stream = method(session_id) | |
82 | self.zmq_stream.on_recv(self._on_zmq_reply) |
|
82 | self.zmq_stream.on_recv(self._on_zmq_reply) | |
83 |
|
83 | |||
84 | def on_message(self, msg): |
|
84 | def on_message(self, msg): | |
85 | logging.info("Message received: %r, %r" % (msg, self.__class__)) |
|
85 | logging.info("Message received: %r, %r" % (msg, self.__class__)) | |
86 | logging.info(self.zmq_stream) |
|
86 | logging.info(self.zmq_stream) | |
87 | self.zmq_stream.send_unicode(msg) |
|
87 | self.zmq_stream.send_unicode(msg) | |
88 |
|
88 | |||
89 | def on_close(self): |
|
89 | def on_close(self): | |
90 | self.zmq_stream.close() |
|
90 | self.zmq_stream.close() | |
91 |
|
91 | |||
92 | def _on_zmq_reply(self, msg_list): |
|
92 | def _on_zmq_reply(self, msg_list): | |
93 | for msg in msg_list: |
|
93 | for msg in msg_list: | |
94 | logging.info("Message reply: %r" % msg) |
|
94 | logging.info("Message reply: %r" % msg) | |
95 | self.write_message(msg) |
|
95 | self.write_message(msg) | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | class IOPubStreamHandler(ZMQStreamHandler): |
|
98 | class IOPubStreamHandler(ZMQStreamHandler): | |
99 |
|
99 | |||
100 | stream_name = 'iopub' |
|
100 | stream_name = 'iopub' | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | class ShellStreamHandler(ZMQStreamHandler): |
|
103 | class ShellStreamHandler(ZMQStreamHandler): | |
104 |
|
104 | |||
105 | stream_name = 'shell' |
|
105 | stream_name = 'shell' | |
106 |
|
106 | |||
107 |
|
107 | |||
108 | class NotebookRootHandler(web.RequestHandler): |
|
108 | class NotebookRootHandler(web.RequestHandler): | |
109 |
|
109 | |||
110 | def get(self): |
|
110 | def get(self): | |
111 | files = os.listdir(os.getcwd()) |
|
111 | files = os.listdir(os.getcwd()) | |
112 | files = [file for file in files if file.endswith(".nb")] |
|
112 | files = [file for file in files if file.endswith(".nb")] | |
113 | self.write(json.dumps(files)) |
|
113 | self.write(json.dumps(files)) | |
114 |
|
114 | |||
115 |
|
115 | |||
116 | class NotebookHandler(web.RequestHandler): |
|
116 | class NotebookHandler(web.RequestHandler): | |
117 |
|
117 | |||
118 | SUPPORTED_METHODS = ("GET", "DELETE", "PUT") |
|
118 | SUPPORTED_METHODS = ("GET", "DELETE", "PUT") | |
119 |
|
119 | |||
120 | def find_path(self, filename): |
|
120 | def find_path(self, filename): | |
121 | filename = urllib.unquote(filename) + ".nb" |
|
121 | filename = urllib.unquote(filename) + ".nb" | |
122 | path = os.path.join(os.getcwd(), filename) |
|
122 | path = os.path.join(os.getcwd(), filename) | |
123 | return path |
|
123 | return path | |
124 |
|
124 | |||
125 | def get(self, filename): |
|
125 | def get(self, filename): | |
126 | path = self.find_path(filename) |
|
126 | path = self.find_path(filename) | |
127 | if not os.path.isfile(path): |
|
127 | if not os.path.isfile(path): | |
128 | raise web.HTTPError(404) |
|
128 | raise web.HTTPError(404) | |
129 | info = os.stat(path) |
|
129 | info = os.stat(path) | |
130 | self.set_header("Content-Type", "application/unknown") |
|
130 | self.set_header("Content-Type", "application/unknown") | |
131 | self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp( |
|
131 | self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp( | |
132 | info.st_mtime)) |
|
132 | info.st_mtime)) | |
133 | f = open(path, "r") |
|
133 | f = open(path, "r") | |
134 | try: |
|
134 | try: | |
135 | self.finish(f.read()) |
|
135 | self.finish(f.read()) | |
136 | finally: |
|
136 | finally: | |
137 | f.close() |
|
137 | f.close() | |
138 |
|
138 | |||
139 | def put(self, filename): |
|
139 | def put(self, filename): | |
140 | path = self.find_path(filename) |
|
140 | path = self.find_path(filename) | |
141 | f = open(path, "w") |
|
141 | f = open(path, "w") | |
142 | f.write(self.request.body) |
|
142 | f.write(self.request.body) | |
143 | f.close() |
|
143 | f.close() | |
144 | self.finish() |
|
144 | self.finish() | |
145 |
|
145 | |||
146 | def delete(self, filename): |
|
146 | def delete(self, filename): | |
147 | path = self.find_path(filename) |
|
147 | path = self.find_path(filename) | |
148 | if not os.path.isfile(path): |
|
148 | if not os.path.isfile(path): | |
149 | raise web.HTTPError(404) |
|
149 | raise web.HTTPError(404) | |
150 | os.unlink(path) |
|
150 | os.unlink(path) | |
151 | self.set_status(204) |
|
151 | self.set_status(204) | |
152 | self.finish() |
|
152 | self.finish() | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | class NotebookApplication(web.Application): |
|
155 | class NotebookApplication(web.Application): | |
156 |
|
156 | |||
157 | def __init__(self): |
|
157 | def __init__(self): | |
158 | handlers = [ |
|
158 | handlers = [ | |
159 | (r"/", MainHandler), |
|
159 | (r"/", MainHandler), | |
160 | (r"/kernels/%s" % (_kernel_id_regex,), KernelHandler), |
|
160 | (r"/kernels/%s" % (_kernel_id_regex,), KernelHandler), | |
161 | (r"/kernels/%s/sessions" % (_kernel_id_regex,), SessionHandler), |
|
161 | (r"/kernels/%s/sessions" % (_kernel_id_regex,), SessionHandler), | |
162 | (r"/kernels/%s/sessions/%s/iopub" % (_kernel_id_regex,_session_id_regex), IOPubStreamHandler), |
|
162 | (r"/kernels/%s/sessions/%s/iopub" % (_kernel_id_regex,_session_id_regex), IOPubStreamHandler), | |
163 | (r"/kernels/%s/sessions/%s/shell" % (_kernel_id_regex,_session_id_regex), ShellStreamHandler), |
|
163 | (r"/kernels/%s/sessions/%s/shell" % (_kernel_id_regex,_session_id_regex), ShellStreamHandler), | |
164 | (r"/notebooks", NotebookRootHandler), |
|
164 | (r"/notebooks", NotebookRootHandler), | |
165 | (r"/notebooks/([^/]+)", NotebookHandler) |
|
165 | (r"/notebooks/([^/]+)", NotebookHandler) | |
166 | ] |
|
166 | ] | |
167 | settings = dict( |
|
167 | settings = dict( | |
168 | template_path=os.path.join(os.path.dirname(__file__), "templates"), |
|
168 | template_path=os.path.join(os.path.dirname(__file__), "templates"), | |
169 | static_path=os.path.join(os.path.dirname(__file__), "static"), |
|
169 | static_path=os.path.join(os.path.dirname(__file__), "static"), | |
170 | ) |
|
170 | ) | |
171 | web.Application.__init__(self, handlers, **settings) |
|
171 | web.Application.__init__(self, handlers, **settings) | |
172 | self.context = zmq.Context() |
|
172 | self.context = zmq.Context() | |
173 | self.kernel_manager = KernelManager(self.context) |
|
173 | self.kernel_manager = KernelManager(self.context) | |
174 |
|
174 | |||
175 |
|
175 | |||
176 | def main(): |
|
176 | def main(): | |
177 | options.parse_command_line() |
|
177 | options.parse_command_line() | |
178 | application = NotebookApplication() |
|
178 | application = NotebookApplication() | |
179 | http_server = httpserver.HTTPServer(application) |
|
179 | http_server = httpserver.HTTPServer(application) | |
180 | http_server.listen(options.options.port) |
|
180 | http_server.listen(options.options.port) | |
|
181 | print "IPython Notebook running at: http://127.0.0.1:8888" | |||
181 | ioloop.IOLoop.instance().start() |
|
182 | ioloop.IOLoop.instance().start() | |
182 |
|
183 | |||
183 |
|
184 | |||
184 | if __name__ == "__main__": |
|
185 | if __name__ == "__main__": | |
185 | main() |
|
186 | main() | |
186 |
|
187 |
General Comments 0
You need to be logged in to leave comments.
Login now