Show More
@@ -21,6 +21,7 b' import io' | |||
|
21 | 21 | import os |
|
22 | 22 | import glob |
|
23 | 23 | import shutil |
|
24 | import ast | |
|
24 | 25 | |
|
25 | 26 | from unicodedata import normalize |
|
26 | 27 | |
@@ -74,7 +75,6 b' class FileNotebookManager(NotebookManager):' | |||
|
74 | 75 | |
|
75 | 76 | filename_ext = Unicode(u'.ipynb') |
|
76 | 77 | |
|
77 | rev_mapping = Dict() | |
|
78 | 78 | |
|
79 | 79 | def get_notebook_names(self, path): |
|
80 | 80 | """List all notebook names in the notebook dir.""" |
@@ -87,13 +87,29 b' class FileNotebookManager(NotebookManager):' | |||
|
87 | 87 | |
|
88 | 88 | def list_notebooks(self, path): |
|
89 | 89 | """List all notebooks in the notebook dir.""" |
|
90 | names = self.get_notebook_names(path) | |
|
90 | notebook_names = self.get_notebook_names(path) | |
|
91 | notebook_mapping = [] | |
|
92 | for name in notebook_names: | |
|
93 | model = self.notebook_model(name, path) | |
|
94 | notebook_mapping.append(model) | |
|
95 | return notebook_mapping | |
|
91 | 96 | |
|
92 | data = [] | |
|
93 | for name in names: | |
|
94 | data.append(name) | |
|
95 | #data = sorted(data, key=lambda item: item['name']) | |
|
96 |
r |
|
|
97 | def change_notebook(self, data, notebook_name, notebook_path=None): | |
|
98 | """Changes notebook""" | |
|
99 | full_path = self.get_path(notebook_name, notebook_path) | |
|
100 | changes = data.keys() | |
|
101 | for change in changes: | |
|
102 | if change == "notebook_name": | |
|
103 | os.rename(notebook_name, data['notebook_name']) | |
|
104 | notebook_name = data['notebook_name'] | |
|
105 | if change == "notebook_path": | |
|
106 | new_path = self.get_path(data['notebook_name'], data['notebook_path']) | |
|
107 | stutil.move(old_path, new_path) | |
|
108 | notebook_path = data['notebook_path'] | |
|
109 | if change == "content": | |
|
110 | self.save_notebook(data, notebook_name, notebook_path) | |
|
111 | model = self.notebook_model(notebook_name, notebook_path) | |
|
112 | return model | |
|
97 | 113 | |
|
98 | 114 | def notebook_exists(self, notebook_name): |
|
99 | 115 | """Does a notebook exist?""" |
@@ -103,7 +119,6 b' class FileNotebookManager(NotebookManager):' | |||
|
103 | 119 | path = self.get_path_by_name(self.mapping[notebook_name]) |
|
104 | 120 | return os.path.isfile(path) |
|
105 | 121 | |
|
106 | ||
|
107 | 122 | def get_path(self, notebook_name, notebook_path=None): |
|
108 | 123 | """Return a full path to a notebook given its notebook_name.""" |
|
109 | 124 | return self.get_path_by_name(notebook_name, notebook_path) |
@@ -145,16 +160,16 b' class FileNotebookManager(NotebookManager):' | |||
|
145 | 160 | nb.metadata.name = os.path.splitext(os.path.basename(path))[0] |
|
146 | 161 | return last_modified, nb |
|
147 | 162 | |
|
148 | def write_notebook_object(self, nb, notebook_name=None, notebook_path=None): | |
|
163 | def write_notebook_object(self, nb, notebook_name=None, notebook_path=None, new_name= None): | |
|
149 | 164 | """Save an existing notebook object by notebook_name.""" |
|
150 | try: | |
|
151 | new_name = normalize('NFC', nb.metadata.name) | |
|
152 | except AttributeError: | |
|
153 | raise web.HTTPError(400, u'Missing notebook name') | |
|
154 | ||
|
165 | if new_name == None: | |
|
166 | try: | |
|
167 | new_name = normalize('NFC', nb.metadata.name) | |
|
168 | except AttributeError: | |
|
169 | raise web.HTTPError(400, u'Missing notebook name') | |
|
170 | ||
|
155 | 171 | new_path = notebook_path |
|
156 | 172 | old_name = notebook_name |
|
157 | # old_name = self.mapping[notebook_name] | |
|
158 | 173 | old_checkpoints = self.list_checkpoints(old_name) |
|
159 | 174 | |
|
160 | 175 | path = self.get_path_by_name(new_name, new_path) |
@@ -17,6 +17,7 b' Authors:' | |||
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from tornado import web |
|
20 | import ast | |
|
20 | 21 | |
|
21 | 22 | from zmq.utils import jsonapi |
|
22 | 23 | |
@@ -35,11 +36,7 b' class NotebookRootHandler(IPythonHandler):' | |||
|
35 | 36 | def get(self): |
|
36 | 37 | nbm = self.notebook_manager |
|
37 | 38 | km = self.kernel_manager |
|
38 |
notebook |
|
|
39 | notebooks = [] | |
|
40 | for name in notebook_names: | |
|
41 | model = nbm.notebook_model(name) | |
|
42 | notebooks.append(model) | |
|
39 | notebooks = nbm.list_notebooks("") | |
|
43 | 40 | self.finish(jsonapi.dumps(notebooks)) |
|
44 | 41 | |
|
45 | 42 | @web.authenticated |
@@ -60,7 +57,7 b' class NotebookRootRedirect(IPythonHandler):' | |||
|
60 | 57 | |
|
61 | 58 | class NotebookHandler(IPythonHandler): |
|
62 | 59 | |
|
63 | SUPPORTED_METHODS = ('GET', 'PUT', 'DELETE') | |
|
60 | SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'DELETE') | |
|
64 | 61 | |
|
65 | 62 | @web.authenticated |
|
66 | 63 | def get(self, notebook_path): |
@@ -68,11 +65,7 b' class NotebookHandler(IPythonHandler):' | |||
|
68 | 65 | name, path = nbm.named_notebook_path(notebook_path) |
|
69 | 66 | |
|
70 | 67 | if name == None: |
|
71 |
notebook |
|
|
72 | notebooks = [] | |
|
73 | for name in notebook_names: | |
|
74 | model = nbm.notebook_model(name,path) | |
|
75 | notebooks.append(model) | |
|
68 | notebooks = nbm.list_notebooks(path) | |
|
76 | 69 | self.finish(jsonapi.dumps(notebooks)) |
|
77 | 70 | else: |
|
78 | 71 | format = self.get_argument('format', default='json') |
@@ -89,6 +82,15 b' class NotebookHandler(IPythonHandler):' | |||
|
89 | 82 | self.finish(jsonapi.dumps(model)) |
|
90 | 83 | |
|
91 | 84 | @web.authenticated |
|
85 | def patch(self, notebook_path): | |
|
86 | nbm = self.notebook_manager | |
|
87 | notebook_name, notebook_path = nbm.named_notebook_path(notebook_path) | |
|
88 | data = jsonapi.loads(self.request.body) | |
|
89 | model = nbm.change_notebook(data, notebook_name, notebook_path) | |
|
90 | self.log.info(model) | |
|
91 | self.finish(jsonapi.dumps(model)) | |
|
92 | ||
|
93 | @web.authenticated | |
|
92 | 94 | def put(self, notebook_path): |
|
93 | 95 | nbm = self.notebook_manager |
|
94 | 96 | notebook_name, notebook_path = nbm.named_notebook_path(notebook_path) |
@@ -150,17 +150,17 b' class NotebookManager(LoggingConfigurable):' | |||
|
150 | 150 | raise web.HTTPError(400, u'Invalid JSON data') |
|
151 | 151 | |
|
152 | 152 | if name is None: |
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
153 | try: | |
|
154 | name = nb.metadata.name | |
|
155 | except AttributeError: | |
|
156 | raise web.HTTPError(400, u'Missing notebook name') | |
|
157 | 157 | nb.metadata.name = name |
|
158 | 158 | |
|
159 | 159 | notebook_name = self.write_notebook_object(nb, notebook_path=notebook_path) |
|
160 | 160 | return notebook_name |
|
161 | 161 | |
|
162 | def save_notebook(self, data, notebook_path=None, name=None, format=u'json'): | |
|
163 |
"""Save an existing notebook by notebook_ |
|
|
162 | def save_notebook(self, data, notebook_path=None, name=None, new_name=None, format=u'json'): | |
|
163 | """Save an existing notebook by notebook_name.""" | |
|
164 | 164 | if format not in self.allowed_formats: |
|
165 | 165 | raise web.HTTPError(415, u'Invalid notebook format: %s' % format) |
|
166 | 166 | |
@@ -171,9 +171,9 b' class NotebookManager(LoggingConfigurable):' | |||
|
171 | 171 | |
|
172 | 172 | if name is not None: |
|
173 | 173 | nb.metadata.name = name |
|
174 | self.write_notebook_object(nb, name, notebook_path) | |
|
174 | self.write_notebook_object(nb, name, notebook_path, new_name) | |
|
175 | 175 | |
|
176 | def write_notebook_object(self, nb, notebook_name=None, notebook_path=None): | |
|
176 | def write_notebook_object(self, nb, notebook_name=None, notebook_path=None, new_name=None): | |
|
177 | 177 | """Write a notebook object and return its notebook_name. |
|
178 | 178 | |
|
179 | 179 | If notebook_name is None, this method should create a new notebook_name. |
@@ -59,26 +59,27 b' class SessionRootHandler(IPythonHandler):' | |||
|
59 | 59 | |
|
60 | 60 | class SessionHandler(IPythonHandler): |
|
61 | 61 | |
|
62 | @web.authenticated | |
|
62 | SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE') | |
|
63 | ||
|
64 | @authenticate_unless_readonly | |
|
63 | 65 | def get(self, session_id): |
|
64 | 66 | sm = self.session_manager |
|
65 | 67 | model = sm.get_session_from_id(session_id) |
|
66 | 68 | self.finish(jsonapi.dumps(model)) |
|
67 | ||
|
68 | ||
|
69 | @authenticate_unless_readonly | |
|
70 | def put(self, session_id): | |
|
69 | ||
|
70 | @web.authenticated | |
|
71 | def patch(self, session_id): | |
|
71 | 72 | sm = self.session_manager |
|
72 | 73 | nbm = self.notebook_manager |
|
73 | 74 | km = self.kernel_manager |
|
74 |
notebook_path = self. |
|
|
75 | notebook_path = self.request.body | |
|
75 | 76 | notebook_name, path = nbm.named_notebook_path(notebook_path) |
|
76 | 77 | kernel_id = sm.get_kernel_from_session(session_id) |
|
77 | 78 | kernel = km.kernel_model(kernel_id, self.ws_url) |
|
78 | 79 | sm.delete_mapping_for_session(session_id) |
|
79 | 80 | model = sm.session_model(session_id, notebook_name, path, kernel) |
|
80 | 81 | self.finish(jsonapi.dumps(model)) |
|
81 | ||
|
82 | ||
|
82 | 83 | @web.authenticated |
|
83 | 84 | def delete(self, session_id): |
|
84 | 85 | sm = self.session_manager |
@@ -1664,7 +1664,7 b' var IPython = (function (IPython) {' | |||
|
1664 | 1664 | |
|
1665 | 1665 | // time the ajax call for autosave tuning purposes. |
|
1666 | 1666 | var start = new Date().getTime(); |
|
1667 | ||
|
1667 | console.log(JSON.stringify(data)) | |
|
1668 | 1668 | // We do the call with settings so we can set cache to false. |
|
1669 | 1669 | var settings = { |
|
1670 | 1670 | processData : false, |
@@ -1730,6 +1730,32 b' var IPython = (function (IPython) {' | |||
|
1730 | 1730 | Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) { |
|
1731 | 1731 | $([IPython.events]).trigger('notebook_save_failed.Notebook'); |
|
1732 | 1732 | }; |
|
1733 | ||
|
1734 | ||
|
1735 | Notebook.prototype.notebook_rename = function (new_name) { | |
|
1736 | var that = this; | |
|
1737 | var name = {'notebook_name': new_name}; | |
|
1738 | var settings = { | |
|
1739 | processData : false, | |
|
1740 | cache : false, | |
|
1741 | type : "PATCH", | |
|
1742 | data : JSON.stringify(name), | |
|
1743 | dataType: "json", | |
|
1744 | headers : {'Content-Type': 'application/json'}, | |
|
1745 | success : $.proxy(that.rename_success, this) | |
|
1746 | }; | |
|
1747 | $([IPython.events]).trigger('notebook_rename.Notebook'); | |
|
1748 | var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebookPath()+ this.notebook_name; | |
|
1749 | $.ajax(url, settings); | |
|
1750 | }; | |
|
1751 | ||
|
1752 | ||
|
1753 | Notebook.prototype.rename_success = function (json, status, xhr) { | |
|
1754 | this.notebook_name = json.notebook_name | |
|
1755 | var notebook_path = this.notebookPath() + this.notebook_name; | |
|
1756 | this.session.notebook_rename(notebook_path); | |
|
1757 | $([IPython.events]).trigger('notebook_renamed.Notebook'); | |
|
1758 | } | |
|
1733 | 1759 | |
|
1734 | 1760 | /** |
|
1735 | 1761 | * Request a notebook's data from the server. |
@@ -46,6 +46,10 b' var IPython = (function (IPython) {' | |||
|
46 | 46 | that.update_notebook_name(); |
|
47 | 47 | that.update_document_title(); |
|
48 | 48 | }); |
|
49 | $([IPython.events]).on('notebook_renamed.Notebook', function () { | |
|
50 | that.update_notebook_name(); | |
|
51 | that.update_document_title(); | |
|
52 | }); | |
|
49 | 53 | $([IPython.events]).on('notebook_save_failed.Notebook', function () { |
|
50 | 54 | that.set_save_status('Autosave Failed!'); |
|
51 | 55 | }); |
@@ -90,8 +94,7 b' var IPython = (function (IPython) {' | |||
|
90 | 94 | ); |
|
91 | 95 | return false; |
|
92 | 96 | } else { |
|
93 |
IPython.notebook. |
|
|
94 | IPython.notebook.save_notebook(); | |
|
97 | IPython.notebook.notebook_rename(new_name); | |
|
95 | 98 | } |
|
96 | 99 | }} |
|
97 | 100 | }, |
@@ -112,6 +115,7 b' var IPython = (function (IPython) {' | |||
|
112 | 115 | |
|
113 | 116 | SaveWidget.prototype.update_notebook_name = function () { |
|
114 | 117 | var nbname = IPython.notebook.get_notebook_name(); |
|
118 | console.log("UPDATED") | |
|
115 | 119 | this.element.find('span#notebook_name').html(nbname); |
|
116 | 120 | }; |
|
117 | 121 |
@@ -30,8 +30,33 b' var IPython = (function (IPython) {' | |||
|
30 | 30 | ); |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | Session.prototype.notebook_rename = function (notebook_path) { | |
|
34 | this.notebook_path = notebook_path; | |
|
35 | console.log("TEST"); | |
|
36 | var settings = { | |
|
37 | processData : false, | |
|
38 | cache : false, | |
|
39 | type : "PATCH", | |
|
40 | data: notebook_path, | |
|
41 | dataType : "json", | |
|
42 | }; | |
|
43 | var url = this._baseProjectUrl + 'api/sessions/' + this.session_id; | |
|
44 | $.ajax(url, settings); | |
|
45 | } | |
|
46 | ||
|
47 | ||
|
48 | Session.prototype.delete_session = function() { | |
|
49 | var settings = { | |
|
50 | processData : false, | |
|
51 | cache : false, | |
|
52 | type : "DELETE", | |
|
53 | dataType : "json", | |
|
54 | }; | |
|
55 | var url = this._baseProjectUrl + 'api/sessions/' + this.session_id; | |
|
56 | $.ajax(url, settings); | |
|
57 | }; | |
|
58 | ||
|
33 | 59 | // Kernel related things |
|
34 | ||
|
35 | 60 | /** |
|
36 | 61 | * Start a new kernel and set it on each code cell. |
|
37 | 62 | * |
@@ -67,16 +92,6 b' var IPython = (function (IPython) {' | |||
|
67 | 92 | this.kernel.interrupt(); |
|
68 | 93 | }; |
|
69 | 94 | |
|
70 | Session.prototype.delete_session = function() { | |
|
71 | var settings = { | |
|
72 | processData : false, | |
|
73 | cache : false, | |
|
74 | type : "DELETE", | |
|
75 | dataType : "json", | |
|
76 | }; | |
|
77 | var url = this._baseProjectUrl + 'api/sessions/' + this.session_id; | |
|
78 | $.ajax(url, settings); | |
|
79 | }; | |
|
80 | 95 | |
|
81 | 96 | Session.prototype.kill_kernel = function() { |
|
82 | 97 | this.kernel.kill(); |
General Comments 0
You need to be logged in to leave comments.
Login now