##// END OF EJS Templates
Return a proper JSON object
Jessica B. Hamrick -
Show More
@@ -1,122 +1,124 b''
1 1 """Tornado handlers for the sessions web service."""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import json
7 7
8 8 from tornado import web
9 9
10 10 from ...base.handlers import IPythonHandler, json_errors
11 11 from IPython.utils.jsonutil import date_default
12 12 from IPython.html.utils import url_path_join, url_escape
13 13 from IPython.kernel.kernelspec import NoSuchKernel
14 14
15 15
16 16 class SessionRootHandler(IPythonHandler):
17 17
18 18 @web.authenticated
19 19 @json_errors
20 20 def get(self):
21 21 # Return a list of running sessions
22 22 sm = self.session_manager
23 23 sessions = sm.list_sessions()
24 24 self.finish(json.dumps(sessions, default=date_default))
25 25
26 26 @web.authenticated
27 27 @json_errors
28 28 def post(self):
29 29 # Creates a new session
30 30 #(unless a session already exists for the named nb)
31 31 sm = self.session_manager
32 32 cm = self.contents_manager
33 33 km = self.kernel_manager
34 34
35 35 model = self.get_json_body()
36 36 if model is None:
37 37 raise web.HTTPError(400, "No JSON data provided")
38 38 try:
39 39 name = model['notebook']['name']
40 40 except KeyError:
41 41 raise web.HTTPError(400, "Missing field in JSON data: notebook.name")
42 42 try:
43 43 path = model['notebook']['path']
44 44 except KeyError:
45 45 raise web.HTTPError(400, "Missing field in JSON data: notebook.path")
46 46 try:
47 47 kernel_name = model['kernel']['name']
48 48 except KeyError:
49 49 self.log.debug("No kernel name specified, using default kernel")
50 50 kernel_name = None
51 51
52 52 # Check to see if session exists
53 53 if sm.session_exists(name=name, path=path):
54 54 model = sm.get_session(name=name, path=path)
55 55 else:
56 56 try:
57 57 model = sm.create_session(name=name, path=path, kernel_name=kernel_name)
58 58 except NoSuchKernel:
59 59 msg = ("The '%s' kernel is not available. Please pick another "
60 60 "suitable kernel instead, or install that kernel." % kernel_name)
61 61 status_msg = 'Kernel not found'
62 msg = dict(full=msg, short=status_msg)
63 raise web.HTTPError(501, json.dumps(msg))
62 self.log.warn('Kernel not found: %s' % kernel_name)
63 self.set_status(501)
64 self.finish(json.dumps(dict(message=msg, short_message=status_msg)))
65 return
64 66
65 67 location = url_path_join(self.base_url, 'api', 'sessions', model['id'])
66 68 self.set_header('Location', url_escape(location))
67 69 self.set_status(201)
68 70 self.finish(json.dumps(model, default=date_default))
69 71
70 72 class SessionHandler(IPythonHandler):
71 73
72 74 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
73 75
74 76 @web.authenticated
75 77 @json_errors
76 78 def get(self, session_id):
77 79 # Returns the JSON model for a single session
78 80 sm = self.session_manager
79 81 model = sm.get_session(session_id=session_id)
80 82 self.finish(json.dumps(model, default=date_default))
81 83
82 84 @web.authenticated
83 85 @json_errors
84 86 def patch(self, session_id):
85 87 # Currently, this handler is strictly for renaming notebooks
86 88 sm = self.session_manager
87 89 model = self.get_json_body()
88 90 if model is None:
89 91 raise web.HTTPError(400, "No JSON data provided")
90 92 changes = {}
91 93 if 'notebook' in model:
92 94 notebook = model['notebook']
93 95 if 'name' in notebook:
94 96 changes['name'] = notebook['name']
95 97 if 'path' in notebook:
96 98 changes['path'] = notebook['path']
97 99
98 100 sm.update_session(session_id, **changes)
99 101 model = sm.get_session(session_id=session_id)
100 102 self.finish(json.dumps(model, default=date_default))
101 103
102 104 @web.authenticated
103 105 @json_errors
104 106 def delete(self, session_id):
105 107 # Deletes the session with given session_id
106 108 sm = self.session_manager
107 109 sm.delete_session(session_id)
108 110 self.set_status(204)
109 111 self.finish()
110 112
111 113
112 114 #-----------------------------------------------------------------------------
113 115 # URL to handler mappings
114 116 #-----------------------------------------------------------------------------
115 117
116 118 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
117 119
118 120 default_handlers = [
119 121 (r"/api/sessions/%s" % _session_id_regex, SessionHandler),
120 122 (r"/api/sessions", SessionRootHandler)
121 123 ]
122 124
General Comments 0
You need to be logged in to leave comments. Login now