Show More
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,105 b'' | |||||
|
1 | """Tornado handlers for the notebooks web service. | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Zach Sailer | |||
|
6 | """ | |||
|
7 | ||||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |||
|
10 | # | |||
|
11 | # Distributed under the terms of the BSD License. The full license is in | |||
|
12 | # the file COPYING, distributed as part of this software. | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Imports | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | from tornado import web | |||
|
20 | ||||
|
21 | from zmq.utils import jsonapi | |||
|
22 | ||||
|
23 | from IPython.utils.jsonutil import date_default | |||
|
24 | ||||
|
25 | from ...base.handlers import IPythonHandler, authenticate_unless_readonly | |||
|
26 | ||||
|
27 | #----------------------------------------------------------------------------- | |||
|
28 | # Session web service handlers | |||
|
29 | #----------------------------------------------------------------------------- | |||
|
30 | ||||
|
31 | ||||
|
32 | ||||
|
33 | class SessionRootHandler(IPythonHandler): | |||
|
34 | ||||
|
35 | ||||
|
36 | @authenticate_unless_readonly | |||
|
37 | def get(self): | |||
|
38 | sm = self.session_manager | |||
|
39 | nbm = self.notebook_manager | |||
|
40 | km = self.kernel_manager | |||
|
41 | sessions = sm.list_sessions() | |||
|
42 | self.finish(jsonapi.dumps(sessions)) | |||
|
43 | ||||
|
44 | ||||
|
45 | @web.authenticated | |||
|
46 | def post(self): | |||
|
47 | sm = self.session_manager | |||
|
48 | nbm = self.notebook_manager | |||
|
49 | km = self.kernel_manager | |||
|
50 | notebook_path = self.get_argument('notebook_path', default=None) | |||
|
51 | notebook_name, path = nbm.named_notebook_path(notebook_path) | |||
|
52 | session_id, model = sm.get_session(notebook_name, path) | |||
|
53 | if model == None: | |||
|
54 | kernel_id = km.start_kernel() | |||
|
55 | kernel = km.kernel_model(kernel_id, self.ws_url) | |||
|
56 | model = sm.session_model(session_id, notebook_name, path, kernel) | |||
|
57 | self.finish(jsonapi.dumps(model)) | |||
|
58 | ||||
|
59 | ||||
|
60 | class SessionHandler(IPythonHandler): | |||
|
61 | ||||
|
62 | @web.authenticated | |||
|
63 | def get(self, session_id): | |||
|
64 | sm = self.session_manager | |||
|
65 | model = sm.get_session_from_id(session_id) | |||
|
66 | self.finish(jsonapi.dumps(model)) | |||
|
67 | ||||
|
68 | ||||
|
69 | @authenticate_unless_readonly | |||
|
70 | def put(self, session_id): | |||
|
71 | sm = self.session_manager | |||
|
72 | nbm = self.notebook_manager | |||
|
73 | km = self.kernel_manager | |||
|
74 | notebook_path = self.get_argument('notebook_path', default=None) | |||
|
75 | notebook_name, path = nbm.named_notebook_path(notebook_path) | |||
|
76 | kernel_id = sm.get_kernel_from_session(session_id) | |||
|
77 | kernel = km.kernel_model(kernel_id, self.ws_url) | |||
|
78 | sm.delete_mapping_for_session(session_id) | |||
|
79 | model = sm.session_model(session_id, notebook_name, path, kernel) | |||
|
80 | return model | |||
|
81 | ||||
|
82 | @web.authenticated | |||
|
83 | def delete(self, session_id): | |||
|
84 | sm = self.session_manager | |||
|
85 | nbm = self.notebook_manager | |||
|
86 | km = self.kernel_manager | |||
|
87 | kernel_id = sm.get_kernel_from_session(session_id) | |||
|
88 | km.shutdown_kernel(kernel_id) | |||
|
89 | sm.delete_mapping_for_session(session_id) | |||
|
90 | ||||
|
91 | ||||
|
92 | #----------------------------------------------------------------------------- | |||
|
93 | # URL to handler mappings | |||
|
94 | #----------------------------------------------------------------------------- | |||
|
95 | ||||
|
96 | _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" | |||
|
97 | ||||
|
98 | default_handlers = [ | |||
|
99 | (r"api/sessions/%s" % _session_id_regex, SessionHandler), | |||
|
100 | (r"api/sessions", SessionRootHandler) | |||
|
101 | ] | |||
|
102 | ||||
|
103 | ||||
|
104 | ||||
|
105 |
@@ -0,0 +1,95 b'' | |||||
|
1 | """A base class session manager. | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Zach Sailer | |||
|
6 | """ | |||
|
7 | ||||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | # Copyright (C) 2011 The IPython Development Team | |||
|
10 | # | |||
|
11 | # Distributed under the terms of the BSD License. The full license is in | |||
|
12 | # the file COPYING, distributed as part of this software. | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Imports | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | import os | |||
|
20 | import uuid | |||
|
21 | ||||
|
22 | from tornado import web | |||
|
23 | ||||
|
24 | from IPython.config.configurable import LoggingConfigurable | |||
|
25 | from IPython.nbformat import current | |||
|
26 | from IPython.utils.traitlets import List, Dict, Unicode, TraitError | |||
|
27 | ||||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | # Classes | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | ||||
|
32 | class SessionManager(LoggingConfigurable): | |||
|
33 | ||||
|
34 | # Use session_ids to map notebook names to kernel_ids | |||
|
35 | sessions = List() | |||
|
36 | ||||
|
37 | def get_session(self, nb_name, nb_path=None): | |||
|
38 | """Get an existing session or create a new one""" | |||
|
39 | model = None | |||
|
40 | for session in self.sessions: | |||
|
41 | if session['notebook_name'] == nb_name and session['notebook_path'] == nb_path: | |||
|
42 | session_id = session['session_id'] | |||
|
43 | model = session | |||
|
44 | if model != None: | |||
|
45 | return session_id, model | |||
|
46 | else: | |||
|
47 | session_id = unicode(uuid.uuid4()) | |||
|
48 | return session_id, model | |||
|
49 | ||||
|
50 | def session_model(self, session_id, notebook_name=None, notebook_path=None, kernel=None): | |||
|
51 | """ Create a session that links notebooks with kernels """ | |||
|
52 | model = dict(session_id=session_id, | |||
|
53 | notebook_name=notebook_name, | |||
|
54 | notebook_path=notebook_path, | |||
|
55 | kernel=kernel) | |||
|
56 | self.sessions.append(model) | |||
|
57 | return model | |||
|
58 | ||||
|
59 | def list_sessions(self): | |||
|
60 | """List all sessions and their information""" | |||
|
61 | return self.sessions | |||
|
62 | ||||
|
63 | def set_kernel_for_sessions(self, session_id, kernel_id): | |||
|
64 | """Maps the kernel_ids to the session_id in session_mapping""" | |||
|
65 | for session in self.sessions: | |||
|
66 | if session['session_id'] == session_id: | |||
|
67 | session['kernel_id'] = kernel_id | |||
|
68 | return self.sessions | |||
|
69 | ||||
|
70 | def delete_mapping_for_session(self, session_id): | |||
|
71 | """Delete the session from session_mapping with the given session_id""" | |||
|
72 | i = 0 | |||
|
73 | for session in self.sessions: | |||
|
74 | if session['session_id'] == session_id: | |||
|
75 | del self.sessions[i] | |||
|
76 | i = i + 1 | |||
|
77 | return self.sessions | |||
|
78 | ||||
|
79 | def get_session_from_id(self, session_id): | |||
|
80 | for session in self.sessions: | |||
|
81 | if session['session_id'] == session_id: | |||
|
82 | return session | |||
|
83 | ||||
|
84 | def get_notebook_from_session(self, session_id): | |||
|
85 | """Returns the notebook_path for the given session_id""" | |||
|
86 | for session in self.sessions: | |||
|
87 | if session['session_id'] == session_id: | |||
|
88 | return session['notebook_name'] | |||
|
89 | ||||
|
90 | def get_kernel_from_session(self, session_id): | |||
|
91 | """Returns the kernel_id for the given session_id""" | |||
|
92 | for session in self.sessions: | |||
|
93 | if session['session_id'] == session_id: | |||
|
94 | return session['kernel']['kernel_id'] | |||
|
95 |
General Comments 0
You need to be logged in to leave comments.
Login now