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