##// END OF EJS Templates
remove unused imports
remove unused imports

File last commit:

r13240:962ba2d0
r14584:27be0fb8
Show More
handlers.py
127 lines | 4.3 KiB | text/x-python | PythonLexer
Zachary Sailer
standard model changes
r13011 """Tornado handlers for the sessions web service.
Zachary Sailer
manual rebase - add sessions web service
r12985
Authors:
* Zach Sailer
"""
#-----------------------------------------------------------------------------
Zachary Sailer
standard model changes
r13011 # Copyright (C) 2013 The IPython Development Team
Zachary Sailer
manual rebase - add sessions web service
r12985 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Zachary Sailer
changes after session manager code review
r13057 import json
Zachary Sailer
manual rebase - add sessions web service
r12985
Zachary Sailer
changes after session manager code review
r13057 from tornado import web
Brian E. Granger
Improving tests and setting of Location header.
r13116
Zachary Sailer
changes after session manager code review
r13057 from ...base.handlers import IPythonHandler, json_errors
Brian E. Granger
Improving tests and setting of Location header.
r13116 from IPython.utils.jsonutil import date_default
MinRK
escape URLs in Location headers
r13132 from IPython.html.utils import url_path_join, url_escape
Zachary Sailer
manual rebase - add sessions web service
r12985
#-----------------------------------------------------------------------------
# Session web service handlers
#-----------------------------------------------------------------------------
class SessionRootHandler(IPythonHandler):
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
rebase master- eliminate read-only
r13014 @web.authenticated
Zachary Sailer
changes after session manager code review
r13057 @json_errors
Zachary Sailer
manual rebase - add sessions web service
r12985 def get(self):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Return a list of running sessions
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
sessions = sm.list_sessions()
Zachary Sailer
changes after session manager code review
r13057 self.finish(json.dumps(sessions, default=date_default))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985 @web.authenticated
Zachary Sailer
changes after session manager code review
r13057 @json_errors
Zachary Sailer
manual rebase - add sessions web service
r12985 def post(self):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Creates a new session
#(unless a session already exists for the named nb)
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
nbm = self.notebook_manager
km = self.kernel_manager
Zachary Sailer
changes after session manager code review
r13057 model = self.get_json_body()
if model is None:
Zachary Sailer
adding to test_kernels_api.py...
r13058 raise web.HTTPError(400, "No JSON data provided")
Zachary Sailer
changes after session manager code review
r13057 try:
name = model['notebook']['name']
except KeyError:
Zachary Sailer
adding to test_kernels_api.py...
r13058 raise web.HTTPError(400, "Missing field in JSON data: name")
Zachary Sailer
changes after session manager code review
r13057 try:
path = model['notebook']['path']
except KeyError:
Zachary Sailer
adding to test_kernels_api.py...
r13058 raise web.HTTPError(400, "Missing field in JSON data: path")
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Check to see if session exists
Zachary Sailer
session manager restructuring...
r13035 if sm.session_exists(name=name, path=path):
model = sm.get_session(name=name, path=path)
else:
MinRK
set kernel cwd to notebook's directory...
r13240 kernel_id = km.start_kernel(cwd=nbm.get_os_path(path))
Zachary Sailer
changes after session manager code review
r13057 model = sm.create_session(name=name, path=path, kernel_id=kernel_id, ws_url=self.ws_url)
Brian E. Granger
Improving tests and setting of Location header.
r13116 location = url_path_join(self.base_kernel_url, 'api', 'sessions', model['id'])
MinRK
escape URLs in Location headers
r13132 self.set_header('Location', url_escape(location))
Thomas Kluyver
Fix some HTTP status codes in sessions API
r13095 self.set_status(201)
Zachary Sailer
changes after session manager code review
r13057 self.finish(json.dumps(model, default=date_default))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985 class SessionHandler(IPythonHandler):
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
rebase master- eliminate read-only
r13014 @web.authenticated
Zachary Sailer
changes after session manager code review
r13057 @json_errors
Zachary Sailer
manual rebase - add sessions web service
r12985 def get(self, session_id):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Returns the JSON model for a single session
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
MinRK
review pass on Sessions API
r13101 model = sm.get_session(session_id=session_id)
Zachary Sailer
changes after session manager code review
r13057 self.finish(json.dumps(model, default=date_default))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 @web.authenticated
Zachary Sailer
changes after session manager code review
r13057 @json_errors
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 def patch(self, session_id):
Zachary Sailer
session manager restructuring...
r13035 # Currently, this handler is strictly for renaming notebooks
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
Zachary Sailer
changes after session manager code review
r13057 model = self.get_json_body()
if model is None:
Thomas Kluyver
Clean up sessions code from static analysis
r13096 raise web.HTTPError(400, "No JSON data provided")
Zachary Sailer
changes after session manager code review
r13057 changes = {}
if 'notebook' in model:
notebook = model['notebook']
if 'name' in notebook:
changes['name'] = notebook['name']
if 'path' in notebook:
changes['path'] = notebook['path']
MinRK
review pass on Sessions API
r13101
Zachary Sailer
changes after session manager code review
r13057 sm.update_session(session_id, **changes)
MinRK
review pass on Sessions API
r13101 model = sm.get_session(session_id=session_id)
Zachary Sailer
changes after session manager code review
r13057 self.finish(json.dumps(model, default=date_default))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985 @web.authenticated
Zachary Sailer
changes after session manager code review
r13057 @json_errors
Zachary Sailer
manual rebase - add sessions web service
r12985 def delete(self, session_id):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Deletes the session with given session_id
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
km = self.kernel_manager
MinRK
review pass on Sessions API
r13101 session = sm.get_session(session_id=session_id)
sm.delete_session(session_id)
Zachary Sailer
session manager restructuring...
r13035 km.shutdown_kernel(session['kernel']['id'])
Zachary Sailer
fixed shutdown button refresh on dashboard
r12996 self.set_status(204)
self.finish()
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
_session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
default_handlers = [
Zachary Sailer
add tests to notebooks api...
r13059 (r"/api/sessions/%s" % _session_id_regex, SessionHandler),
(r"/api/sessions", SessionRootHandler)
Zachary Sailer
manual rebase - add sessions web service
r12985 ]