handlers.py
166 lines
| 4.5 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r4301 | import datetime | ||
Brian Granger
|
r4297 | import json | ||
import logging | ||||
Brian Granger
|
r4292 | import os | ||
Brian Granger
|
r4301 | import urllib | ||
Brian Granger
|
r4306 | import uuid | ||
from Queue import Queue | ||||
Brian Granger
|
r4297 | |||
from tornado import options | ||||
from tornado import web | ||||
from tornado import websocket | ||||
Brian Granger
|
r4292 | |||
Brian Granger
|
r4297 | options.define("port", default=8888, help="run on the given port", type=int) | ||
Brian Granger
|
r4292 | |||
Brian Granger
|
r4306 | _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)" | ||
Brian Granger
|
r4309 | _kernel_action_regex = r"(?P<action>restart|interrupt)" | ||
Brian Granger
|
r4297 | |||
class MainHandler(web.RequestHandler): | ||||
Brian Granger
|
r4292 | def get(self): | ||
self.render('notebook.html') | ||||
Brian Granger
|
r4306 | class KernelHandler(web.RequestHandler): | ||
Brian Granger
|
r4297 | |||
def get(self): | ||||
Brian Granger
|
r4306 | self.write(json.dumps(self.application.kernel_ids)) | ||
Brian Granger
|
r4298 | |||
Brian Granger
|
r4306 | def post(self): | ||
kernel_id = self.application.start_kernel() | ||||
Brian Granger
|
r4298 | self.write(json.dumps(kernel_id)) | ||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4308 | class KernelActionHandler(web.RequestHandler): | ||
Brian Granger
|
r4309 | def post(self, kernel_id, action): | ||
Brian Granger
|
r4308 | # TODO: figure out a better way of handling RPC style calls. | ||
Brian Granger
|
r4309 | if action == 'interrupt': | ||
Brian Granger
|
r4308 | self.application.interrupt_kernel(kernel_id) | ||
Brian Granger
|
r4309 | if action == 'restart': | ||
Brian Granger
|
r4308 | new_kernel_id = self.application.restart_kernel(kernel_id) | ||
self.write(json.dumps(new_kernel_id)) | ||||
Brian Granger
|
r4306 | class ZMQStreamRouter(object): | ||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def __init__(self, zmq_stream): | ||
self.zmq_stream = zmq_stream | ||||
self._clients = {} | ||||
self.zmq_stream.on_recv(self._on_zmq_reply) | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def register_client(self, client): | ||
client_id = uuid.uuid4() | ||||
self._clients[client_id] = client | ||||
return client_id | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def unregister_client(self, client_id): | ||
del self._clients[client_id] | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4308 | def copy_clients(self, router): | ||
# Copy the clients of another router. | ||||
for client_id, client in router._clients.items(): | ||||
client.router = self | ||||
self._clients[client_id] = client | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | class IOPubStreamRouter(ZMQStreamRouter): | ||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def _on_zmq_reply(self, msg_list): | ||
for client_id, client in self._clients.items(): | ||||
for msg in msg_list: | ||||
client.write_message(msg) | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def forward_unicode(self, client_id, msg): | ||
# This is a SUB stream that we should never write to. | ||||
pass | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | |||
class ShellStreamRouter(ZMQStreamRouter): | ||||
def __init__(self, zmq_stream): | ||||
ZMQStreamRouter.__init__(self, zmq_stream) | ||||
self._request_queue = Queue() | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4299 | def _on_zmq_reply(self, msg_list): | ||
Brian Granger
|
r4306 | client_id = self._request_queue.get(block=False) | ||
client = self._clients.get(client_id) | ||||
if client is not None: | ||||
for msg in msg_list: | ||||
client.write_message(msg) | ||||
def forward_unicode(self, client_id, msg): | ||||
self._request_queue.put(client_id) | ||||
self.zmq_stream.send_unicode(msg) | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | class ZMQStreamHandler(websocket.WebSocketHandler): | ||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def initialize(self, stream_name): | ||
self.stream_name = stream_name | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def open(self, kernel_id): | ||
self.router = self.application.get_router(kernel_id, self.stream_name) | ||||
self.client_id = self.router.register_client(self) | ||||
logging.info("Connection open: %s, %s" % (kernel_id, self.client_id)) | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def on_message(self, msg): | ||
self.router.forward_unicode(self.client_id, msg) | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4306 | def on_close(self): | ||
self.router.unregister_client(self.client_id) | ||||
logging.info("Connection closed: %s" % self.client_id) | ||||
Brian Granger
|
r4297 | |||
Brian Granger
|
r4301 | class NotebookRootHandler(web.RequestHandler): | ||
def get(self): | ||||
files = os.listdir(os.getcwd()) | ||||
Brian Granger
|
r4315 | files = [file for file in files if file.endswith(".ipynb")] | ||
Brian Granger
|
r4301 | self.write(json.dumps(files)) | ||
class NotebookHandler(web.RequestHandler): | ||||
SUPPORTED_METHODS = ("GET", "DELETE", "PUT") | ||||
def find_path(self, filename): | ||||
Brian Granger
|
r4315 | filename = urllib.unquote(filename) | ||
if not filename.endswith('.ipynb'): | ||||
raise web.HTTPError(400) | ||||
Brian Granger
|
r4301 | path = os.path.join(os.getcwd(), filename) | ||
return path | ||||
def get(self, filename): | ||||
path = self.find_path(filename) | ||||
if not os.path.isfile(path): | ||||
raise web.HTTPError(404) | ||||
info = os.stat(path) | ||||
self.set_header("Content-Type", "application/unknown") | ||||
self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp( | ||||
info.st_mtime)) | ||||
f = open(path, "r") | ||||
try: | ||||
self.finish(f.read()) | ||||
finally: | ||||
f.close() | ||||
def put(self, filename): | ||||
path = self.find_path(filename) | ||||
f = open(path, "w") | ||||
f.write(self.request.body) | ||||
f.close() | ||||
self.finish() | ||||
def delete(self, filename): | ||||
path = self.find_path(filename) | ||||
if not os.path.isfile(path): | ||||
raise web.HTTPError(404) | ||||
os.unlink(path) | ||||
self.set_status(204) | ||||
self.finish() | ||||
Brian E. Granger
|
r4340 | |||
Brian Granger
|
r4292 | |||