Show More
@@ -41,12 +41,12 b' class NamedNotebookHandler(web.RequestHandler):' | |||||
41 | class KernelHandler(web.RequestHandler): |
|
41 | class KernelHandler(web.RequestHandler): | |
42 |
|
42 | |||
43 | def get(self): |
|
43 | def get(self): | |
44 |
self. |
|
44 | self.finish(json.dumps(self.application.kernel_ids)) | |
45 |
|
45 | |||
46 | def post(self): |
|
46 | def post(self): | |
47 | kernel_id = self.application.start_kernel() |
|
47 | kernel_id = self.application.start_kernel() | |
48 | self.set_header('Location', '/'+kernel_id) |
|
48 | self.set_header('Location', '/'+kernel_id) | |
49 |
self. |
|
49 | self.finish(json.dumps(kernel_id)) | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | class KernelActionHandler(web.RequestHandler): |
|
52 | class KernelActionHandler(web.RequestHandler): | |
@@ -58,6 +58,7 b' class KernelActionHandler(web.RequestHandler):' | |||||
58 | if action == 'restart': |
|
58 | if action == 'restart': | |
59 | new_kernel_id = self.application.restart_kernel(kernel_id) |
|
59 | new_kernel_id = self.application.restart_kernel(kernel_id) | |
60 | self.write(json.dumps(new_kernel_id)) |
|
60 | self.write(json.dumps(new_kernel_id)) | |
|
61 | self.finish() | |||
61 |
|
62 | |||
62 |
|
63 | |||
63 | class ZMQStreamHandler(websocket.WebSocketHandler): |
|
64 | class ZMQStreamHandler(websocket.WebSocketHandler): | |
@@ -83,7 +84,7 b' class NotebookRootHandler(web.RequestHandler):' | |||||
83 | def get(self): |
|
84 | def get(self): | |
84 | nbm = self.application.notebook_manager |
|
85 | nbm = self.application.notebook_manager | |
85 | files = nbm.list_notebooks() |
|
86 | files = nbm.list_notebooks() | |
86 |
self. |
|
87 | self.finish(json.dumps(files)) | |
87 |
|
88 | |||
88 | def post(self): |
|
89 | def post(self): | |
89 | nbm = self.application.notebook_manager |
|
90 | nbm = self.application.notebook_manager | |
@@ -95,7 +96,7 b' class NotebookRootHandler(web.RequestHandler):' | |||||
95 | else: |
|
96 | else: | |
96 | notebook_id = nbm.new_notebook() |
|
97 | notebook_id = nbm.new_notebook() | |
97 | self.set_header('Location', '/'+notebook_id) |
|
98 | self.set_header('Location', '/'+notebook_id) | |
98 |
self. |
|
99 | self.finish(json.dumps(notebook_id)) | |
99 |
|
100 | |||
100 |
|
101 | |||
101 | class NotebookHandler(web.RequestHandler): |
|
102 | class NotebookHandler(web.RequestHandler): | |
@@ -110,10 +111,10 b' class NotebookHandler(web.RequestHandler):' | |||||
110 | self.set_header('Content-Type', 'application/json') |
|
111 | self.set_header('Content-Type', 'application/json') | |
111 | self.set_header('Content-Disposition','attachment; filename=%s.json' % name) |
|
112 | self.set_header('Content-Disposition','attachment; filename=%s.json' % name) | |
112 | elif format == u'xml': |
|
113 | elif format == u'xml': | |
113 |
self.set_header('Content-Type', ' |
|
114 | self.set_header('Content-Type', 'application/xml') | |
114 | self.set_header('Content-Disposition','attachment; filename=%s.ipynb' % name) |
|
115 | self.set_header('Content-Disposition','attachment; filename=%s.ipynb' % name) | |
115 | elif format == u'py': |
|
116 | elif format == u'py': | |
116 |
self.set_header('Content-Type', ' |
|
117 | self.set_header('Content-Type', 'application/x-python') | |
117 | self.set_header('Content-Disposition','attachment; filename=%s.py' % name) |
|
118 | self.set_header('Content-Disposition','attachment; filename=%s.py' % name) | |
118 | self.set_header('Last-Modified', last_mod) |
|
119 | self.set_header('Last-Modified', last_mod) | |
119 | self.finish(data) |
|
120 | self.finish(data) |
@@ -559,11 +559,12 b' var IPython = (function (IPython) {' | |||||
559 | data.id = notebook_id |
|
559 | data.id = notebook_id | |
560 | // We do the call with settings so we can set cache to false. |
|
560 | // We do the call with settings so we can set cache to false. | |
561 | var settings = { |
|
561 | var settings = { | |
562 | processData : false, |
|
562 | processData : false, | |
563 | cache : false, |
|
563 | cache : false, | |
564 | type : "PUT", |
|
564 | type : "PUT", | |
565 | data : JSON.stringify(data), |
|
565 | data : JSON.stringify(data), | |
566 | success : $.proxy(this.notebook_saved,this) |
|
566 | headers : {'Content-Type': 'application/json'}, | |
|
567 | success : $.proxy(this.notebook_saved,this) | |||
567 | }; |
|
568 | }; | |
568 | IPython.save_widget.status_saving(); |
|
569 | IPython.save_widget.status_saving(); | |
569 | $.ajax("/notebooks/" + notebook_id, settings); |
|
570 | $.ajax("/notebooks/" + notebook_id, settings); |
@@ -191,12 +191,21 b' var IPython = (function (IPython) {' | |||||
191 | var nbname = item.find('.item_name > input').attr('value'); |
|
191 | var nbname = item.find('.item_name > input').attr('value'); | |
192 | var nbformat = item.data('nbformat'); |
|
192 | var nbformat = item.data('nbformat'); | |
193 | var nbdata = item.data('nbdata'); |
|
193 | var nbdata = item.data('nbdata'); | |
|
194 | var content_type = 'text/plain'; | |||
|
195 | if (nbformat === 'xml') { | |||
|
196 | content_type = 'application/xml'; | |||
|
197 | } else if (nbformat === 'json') { | |||
|
198 | content_type = 'application/json'; | |||
|
199 | } else if (nbformat === 'py') { | |||
|
200 | content_type = 'application/x-python'; | |||
|
201 | }; | |||
194 | var settings = { |
|
202 | var settings = { | |
195 | processData : false, |
|
203 | processData : false, | |
196 | cache : false, |
|
204 | cache : false, | |
197 |
type : |
|
205 | type : 'POST', | |
198 |
dataType : |
|
206 | dataType : 'json', | |
199 | data : nbdata, |
|
207 | data : nbdata, | |
|
208 | headers : {'Content-Type': content_type}, | |||
200 | success : function (data, status, xhr) { |
|
209 | success : function (data, status, xhr) { | |
201 | that.add_link(data, nbname, item); |
|
210 | that.add_link(data, nbname, item); | |
202 | that.add_delete_button(item); |
|
211 | that.add_delete_button(item); | |
@@ -204,7 +213,7 b' var IPython = (function (IPython) {' | |||||
204 | }; |
|
213 | }; | |
205 |
|
214 | |||
206 | var qs = $.param({name:nbname, format:nbformat}); |
|
215 | var qs = $.param({name:nbname, format:nbformat}); | |
207 |
$.ajax( |
|
216 | $.ajax('/notebooks?' + qs, settings); | |
208 | }); |
|
217 | }); | |
209 | var cancel_button = $('<button>Cancel</button>').button(). |
|
218 | var cancel_button = $('<button>Cancel</button>').button(). | |
210 | click(function (e) { |
|
219 | click(function (e) { |
@@ -43,7 +43,7 b' def parse_py(s, **kwargs):' | |||||
43 | if m is not None: |
|
43 | if m is not None: | |
44 | nbformat = int(m.group('nbformat')) |
|
44 | nbformat = int(m.group('nbformat')) | |
45 | else: |
|
45 | else: | |
46 | raise NBFormatError('No nbformat version found') |
|
46 | nbformat = 2 | |
47 | return nbformat, s |
|
47 | return nbformat, s | |
48 |
|
48 | |||
49 |
|
49 |
@@ -4,6 +4,10 b' from .rwbase import NotebookReader, NotebookWriter' | |||||
4 | from .nbbase import new_code_cell, new_worksheet, new_notebook |
|
4 | from .nbbase import new_code_cell, new_worksheet, new_notebook | |
5 |
|
5 | |||
6 |
|
6 | |||
|
7 | class PyReaderError(Exception): | |||
|
8 | pass | |||
|
9 | ||||
|
10 | ||||
7 | class PyReader(NotebookReader): |
|
11 | class PyReader(NotebookReader): | |
8 |
|
12 | |||
9 | def reads(self, s, **kwargs): |
|
13 | def reads(self, s, **kwargs): | |
@@ -13,20 +17,42 b' class PyReader(NotebookReader):' | |||||
13 | lines = s.splitlines() |
|
17 | lines = s.splitlines() | |
14 | cells = [] |
|
18 | cells = [] | |
15 | cell_lines = [] |
|
19 | cell_lines = [] | |
|
20 | code_cell = False | |||
16 | for line in lines: |
|
21 | for line in lines: | |
17 | if line.startswith(u'# <codecell>'): |
|
22 | if line.startswith(u'# <codecell>'): | |
|
23 | if code_cell: | |||
|
24 | raise PyReaderError('Unexpected <codecell>') | |||
|
25 | if cell_lines: | |||
|
26 | for block in self.split_lines_into_blocks(cell_lines): | |||
|
27 | cells.append(new_code_cell(input=block)) | |||
18 | cell_lines = [] |
|
28 | cell_lines = [] | |
|
29 | code_cell = True | |||
19 | if line.startswith(u'# </codecell>'): |
|
30 | if line.startswith(u'# </codecell>'): | |
|
31 | if not code_cell: | |||
|
32 | raise PyReaderError('Unexpected </codecell>') | |||
20 | code = u'\n'.join(cell_lines) |
|
33 | code = u'\n'.join(cell_lines) | |
21 | code = code.strip(u'\n') |
|
34 | code = code.strip(u'\n') | |
22 | if code: |
|
35 | if code: | |
23 | cells.append(new_code_cell(input=code)) |
|
36 | cells.append(new_code_cell(input=code)) | |
|
37 | code_cell = False | |||
24 | else: |
|
38 | else: | |
25 | cell_lines.append(line) |
|
39 | cell_lines.append(line) | |
|
40 | # For lines we were not able to process, | |||
|
41 | for block in self.split_lines_into_blocks(cell_lines): | |||
|
42 | cells.append(new_code_cell(input=block)) | |||
26 | ws = new_worksheet(cells=cells) |
|
43 | ws = new_worksheet(cells=cells) | |
27 | nb = new_notebook(worksheets=[ws]) |
|
44 | nb = new_notebook(worksheets=[ws]) | |
28 | return nb |
|
45 | return nb | |
29 |
|
46 | |||
|
47 | def split_lines_into_blocks(self, lines): | |||
|
48 | import ast | |||
|
49 | source = '\n'.join(lines) | |||
|
50 | code = ast.parse(source) | |||
|
51 | starts = [x.lineno-1 for x in code.body] | |||
|
52 | for i in range(len(starts)-1): | |||
|
53 | yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n') | |||
|
54 | yield '\n'.join(lines[starts[-1]:]).strip('\n') | |||
|
55 | ||||
30 |
|
56 | |||
31 | class PyWriter(NotebookWriter): |
|
57 | class PyWriter(NotebookWriter): | |
32 |
|
58 |
General Comments 0
You need to be logged in to leave comments.
Login now