handlers.py
112 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
Zachary Sailer
|
r13011 | """Tornado handlers for the sessions web service. | ||
Zachary Sailer
|
r12985 | |||
Authors: | ||||
* Zach Sailer | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
Zachary Sailer
|
r13011 | # Copyright (C) 2013 The IPython Development Team | ||
Zachary Sailer
|
r12985 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
from tornado import web | ||||
from zmq.utils import jsonapi | ||||
from IPython.utils.jsonutil import date_default | ||||
Zachary Sailer
|
r13014 | from ...base.handlers import IPythonHandler | ||
Zachary Sailer
|
r12985 | |||
#----------------------------------------------------------------------------- | ||||
# Session web service handlers | ||||
#----------------------------------------------------------------------------- | ||||
class SessionRootHandler(IPythonHandler): | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r13014 | @web.authenticated | ||
Zachary Sailer
|
r12985 | def get(self): | ||
Zachary Sailer
|
r13036 | # Return a list of running sessions | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
sessions = sm.list_sessions() | ||||
self.finish(jsonapi.dumps(sessions)) | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @web.authenticated | ||
def post(self): | ||||
Zachary Sailer
|
r13036 | # Creates a new session | ||
#(unless a session already exists for the named nb) | ||||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
notebook_path = self.get_argument('notebook_path', default=None) | ||||
Zachary Sailer
|
r13035 | name, path = nbm.named_notebook_path(notebook_path) | ||
Zachary Sailer
|
r13036 | # Check to see if session exists | ||
Zachary Sailer
|
r13035 | if sm.session_exists(name=name, path=path): | ||
model = sm.get_session(name=name, path=path) | ||||
kernel_id = model['kernel']['id'] | ||||
km.start_kernel(kernel_id, cwd=nbm.notebook_dir) | ||||
else: | ||||
session_id = sm.get_session_id() | ||||
sm.save_session(session_id=session_id, name=name, path=path) | ||||
kernel_id = km.start_kernel(cwd=nbm.notebook_dir) | ||||
Zachary Sailer
|
r12985 | kernel = km.kernel_model(kernel_id, self.ws_url) | ||
Zachary Sailer
|
r13035 | sm.update_session(session_id, kernel=kernel_id) | ||
model = sm.get_session(id=session_id) | ||||
self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) | ||||
Zachary Sailer
|
r12985 | self.finish(jsonapi.dumps(model)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | class SessionHandler(IPythonHandler): | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12997 | SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE') | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r13014 | @web.authenticated | ||
Zachary Sailer
|
r12985 | def get(self, session_id): | ||
Zachary Sailer
|
r13036 | # Returns the JSON model for a single session | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
Zachary Sailer
|
r13035 | model = sm.get_session(id=session_id) | ||
Zachary Sailer
|
r12985 | self.finish(jsonapi.dumps(model)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12997 | @web.authenticated | ||
def patch(self, session_id): | ||||
Zachary Sailer
|
r13035 | # Currently, this handler is strictly for renaming notebooks | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
Zachary Sailer
|
r12997 | notebook_path = self.request.body | ||
Zachary Sailer
|
r13035 | name, path = nbm.named_notebook_path(notebook_path) | ||
Zachary Sailer
|
r13036 | sm.update_session(session_id, name=name) | ||
Zachary Sailer
|
r13035 | model = sm.get_session(id=session_id) | ||
Zachary Sailer
|
r12996 | self.finish(jsonapi.dumps(model)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @web.authenticated | ||
def delete(self, session_id): | ||||
Zachary Sailer
|
r13036 | # Deletes the session with given session_id | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
Zachary Sailer
|
r13035 | session = sm.get_session(id=session_id) | ||
sm.delete_session(session_id) | ||||
km.shutdown_kernel(session['kernel']['id']) | ||||
Zachary Sailer
|
r12996 | self.set_status(204) | ||
self.finish() | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | |||
#----------------------------------------------------------------------------- | ||||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
_session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" | ||||
default_handlers = [ | ||||
(r"api/sessions/%s" % _session_id_regex, SessionHandler), | ||||
(r"api/sessions", SessionRootHandler) | ||||
] | ||||