##// END OF EJS Templates
'Download as' script
Thomas Kluyver -
Show More
@@ -1,348 +1,379 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'jquery',
6 6 'base/js/namespace',
7 7 'base/js/dialog',
8 8 'base/js/utils',
9 9 'notebook/js/tour',
10 10 'bootstrap',
11 11 'moment',
12 12 ], function($, IPython, dialog, utils, tour, bootstrap, moment) {
13 13 "use strict";
14 14
15 15 var MenuBar = function (selector, options) {
16 16 // Constructor
17 17 //
18 18 // A MenuBar Class to generate the menubar of IPython notebook
19 19 //
20 20 // Parameters:
21 21 // selector: string
22 22 // options: dictionary
23 23 // Dictionary of keyword arguments.
24 24 // notebook: Notebook instance
25 25 // contents: ContentManager instance
26 26 // layout_manager: LayoutManager instance
27 27 // events: $(Events) instance
28 28 // save_widget: SaveWidget instance
29 29 // quick_help: QuickHelp instance
30 30 // base_url : string
31 31 // notebook_path : string
32 32 // notebook_name : string
33 33 options = options || {};
34 34 this.base_url = options.base_url || utils.get_body_data("baseUrl");
35 35 this.selector = selector;
36 36 this.notebook = options.notebook;
37 37 this.contents = options.contents;
38 38 this.layout_manager = options.layout_manager;
39 39 this.events = options.events;
40 40 this.save_widget = options.save_widget;
41 41 this.quick_help = options.quick_help;
42 42
43 43 try {
44 44 this.tour = new tour.Tour(this.notebook, this.events);
45 45 } catch (e) {
46 46 this.tour = undefined;
47 47 console.log("Failed to instantiate Notebook Tour", e);
48 48 }
49 49
50 50 if (this.selector !== undefined) {
51 51 this.element = $(selector);
52 52 this.style();
53 53 this.bind_events();
54 54 }
55 55 };
56 56
57 57 // TODO: This has definitively nothing to do with style ...
58 58 MenuBar.prototype.style = function () {
59 59 var that = this;
60 60 this.element.find("li").click(function (event, ui) {
61 61 // The selected cell loses focus when the menu is entered, so we
62 62 // re-select it upon selection.
63 63 var i = that.notebook.get_selected_index();
64 64 that.notebook.select(i);
65 65 }
66 66 );
67 67 };
68 68
69 69 MenuBar.prototype._nbconvert = function (format, download) {
70 70 download = download || false;
71 71 var notebook_path = this.notebook.notebook_path;
72 72 if (this.notebook.dirty) {
73 73 this.notebook.save_notebook({async : false});
74 74 }
75 75 var url = utils.url_join_encode(
76 76 this.base_url,
77 77 'nbconvert',
78 78 format,
79 79 notebook_path
80 80 ) + "?download=" + download.toString();
81 81
82 82 window.open(url);
83 83 };
84 84
85 85 MenuBar.prototype.bind_events = function () {
86 86 // File
87 87 var that = this;
88 88 this.element.find('#new_notebook').click(function () {
89 89 var w = window.open();
90 90 // Create a new notebook in the same path as the current
91 91 // notebook's path.
92 92 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
93 93 that.contents.new_untitled(parent, {type: "notebook"}).then(
94 94 function (data) {
95 95 w.location = utils.url_join_encode(
96 96 that.base_url, 'notebooks', data.path
97 97 );
98 98 },
99 99 function(error) {
100 100 w.close();
101 101 dialog.modal({
102 102 title : 'Creating Notebook Failed',
103 103 body : "The error was: " + error.message,
104 104 buttons : {'OK' : {'class' : 'btn-primary'}}
105 105 });
106 106 }
107 107 );
108 108 });
109 109 this.element.find('#open_notebook').click(function () {
110 110 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
111 111 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
112 112 });
113 113 this.element.find('#copy_notebook').click(function () {
114 114 that.notebook.copy_notebook();
115 115 return false;
116 116 });
117 117 this.element.find('#download_ipynb').click(function () {
118 118 var base_url = that.notebook.base_url;
119 119 var notebook_path = that.notebook.notebook_path;
120 120 if (that.notebook.dirty) {
121 121 that.notebook.save_notebook({async : false});
122 122 }
123 123
124 124 var url = utils.url_join_encode(base_url, 'files', notebook_path);
125 125 window.open(url + '?download=1');
126 126 });
127 127
128 128 this.element.find('#print_preview').click(function () {
129 129 that._nbconvert('html', false);
130 130 });
131 131
132 this.element.find('#download_py').click(function () {
133 that._nbconvert('python', true);
134 });
135
136 132 this.element.find('#download_html').click(function () {
137 133 that._nbconvert('html', true);
138 134 });
139 135
140 136 this.element.find('#download_rst').click(function () {
141 137 that._nbconvert('rst', true);
142 138 });
143 139
144 140 this.element.find('#download_pdf').click(function () {
145 141 that._nbconvert('pdf', true);
146 142 });
147 143
148 144 this.element.find('#rename_notebook').click(function () {
149 145 that.save_widget.rename_notebook({notebook: that.notebook});
150 146 });
151 147 this.element.find('#save_checkpoint').click(function () {
152 148 that.notebook.save_checkpoint();
153 149 });
154 150 this.element.find('#restore_checkpoint').click(function () {
155 151 });
156 152 this.element.find('#trust_notebook').click(function () {
157 153 that.notebook.trust_notebook();
158 154 });
159 155 this.events.on('trust_changed.Notebook', function (event, trusted) {
160 156 if (trusted) {
161 157 that.element.find('#trust_notebook')
162 158 .addClass("disabled")
163 159 .find("a").text("Trusted Notebook");
164 160 } else {
165 161 that.element.find('#trust_notebook')
166 162 .removeClass("disabled")
167 163 .find("a").text("Trust Notebook");
168 164 }
169 165 });
170 166 this.element.find('#kill_and_exit').click(function () {
171 167 var close_window = function () {
172 168 // allow closing of new tabs in Chromium, impossible in FF
173 169 window.open('', '_self', '');
174 170 window.close();
175 171 };
176 172 // finish with close on success or failure
177 173 that.notebook.session.delete(close_window, close_window);
178 174 });
179 175 // Edit
180 176 this.element.find('#cut_cell').click(function () {
181 177 that.notebook.cut_cell();
182 178 });
183 179 this.element.find('#copy_cell').click(function () {
184 180 that.notebook.copy_cell();
185 181 });
186 182 this.element.find('#delete_cell').click(function () {
187 183 that.notebook.delete_cell();
188 184 });
189 185 this.element.find('#undelete_cell').click(function () {
190 186 that.notebook.undelete_cell();
191 187 });
192 188 this.element.find('#split_cell').click(function () {
193 189 that.notebook.split_cell();
194 190 });
195 191 this.element.find('#merge_cell_above').click(function () {
196 192 that.notebook.merge_cell_above();
197 193 });
198 194 this.element.find('#merge_cell_below').click(function () {
199 195 that.notebook.merge_cell_below();
200 196 });
201 197 this.element.find('#move_cell_up').click(function () {
202 198 that.notebook.move_cell_up();
203 199 });
204 200 this.element.find('#move_cell_down').click(function () {
205 201 that.notebook.move_cell_down();
206 202 });
207 203 this.element.find('#edit_nb_metadata').click(function () {
208 204 that.notebook.edit_metadata({
209 205 notebook: that.notebook,
210 206 keyboard_manager: that.notebook.keyboard_manager});
211 207 });
212 208
213 209 // View
214 210 this.element.find('#toggle_header').click(function () {
215 211 $('div#header').toggle();
216 212 that.layout_manager.do_resize();
217 213 });
218 214 this.element.find('#toggle_toolbar').click(function () {
219 215 $('div#maintoolbar').toggle();
220 216 that.layout_manager.do_resize();
221 217 });
222 218 // Insert
223 219 this.element.find('#insert_cell_above').click(function () {
224 220 that.notebook.insert_cell_above('code');
225 221 that.notebook.select_prev();
226 222 });
227 223 this.element.find('#insert_cell_below').click(function () {
228 224 that.notebook.insert_cell_below('code');
229 225 that.notebook.select_next();
230 226 });
231 227 // Cell
232 228 this.element.find('#run_cell').click(function () {
233 229 that.notebook.execute_cell();
234 230 });
235 231 this.element.find('#run_cell_select_below').click(function () {
236 232 that.notebook.execute_cell_and_select_below();
237 233 });
238 234 this.element.find('#run_cell_insert_below').click(function () {
239 235 that.notebook.execute_cell_and_insert_below();
240 236 });
241 237 this.element.find('#run_all_cells').click(function () {
242 238 that.notebook.execute_all_cells();
243 239 });
244 240 this.element.find('#run_all_cells_above').click(function () {
245 241 that.notebook.execute_cells_above();
246 242 });
247 243 this.element.find('#run_all_cells_below').click(function () {
248 244 that.notebook.execute_cells_below();
249 245 });
250 246 this.element.find('#to_code').click(function () {
251 247 that.notebook.to_code();
252 248 });
253 249 this.element.find('#to_markdown').click(function () {
254 250 that.notebook.to_markdown();
255 251 });
256 252 this.element.find('#to_raw').click(function () {
257 253 that.notebook.to_raw();
258 254 });
259 255
260 256 this.element.find('#toggle_current_output').click(function () {
261 257 that.notebook.toggle_output();
262 258 });
263 259 this.element.find('#toggle_current_output_scroll').click(function () {
264 260 that.notebook.toggle_output_scroll();
265 261 });
266 262 this.element.find('#clear_current_output').click(function () {
267 263 that.notebook.clear_output();
268 264 });
269 265
270 266 this.element.find('#toggle_all_output').click(function () {
271 267 that.notebook.toggle_all_output();
272 268 });
273 269 this.element.find('#toggle_all_output_scroll').click(function () {
274 270 that.notebook.toggle_all_output_scroll();
275 271 });
276 272 this.element.find('#clear_all_output').click(function () {
277 273 that.notebook.clear_all_output();
278 274 });
279 275
280 276 // Kernel
281 277 this.element.find('#int_kernel').click(function () {
282 278 that.notebook.kernel.interrupt();
283 279 });
284 280 this.element.find('#restart_kernel').click(function () {
285 281 that.notebook.restart_kernel();
286 282 });
287 283 this.element.find('#reconnect_kernel').click(function () {
288 284 that.notebook.kernel.reconnect();
289 285 });
290 286 // Help
291 287 if (this.tour) {
292 288 this.element.find('#notebook_tour').click(function () {
293 289 that.tour.start();
294 290 });
295 291 } else {
296 292 this.element.find('#notebook_tour').addClass("disabled");
297 293 }
298 294 this.element.find('#keyboard_shortcuts').click(function () {
299 295 that.quick_help.show_keyboard_shortcuts();
300 296 });
301 297
302 298 this.update_restore_checkpoint(null);
303 299
304 300 this.events.on('checkpoints_listed.Notebook', function (event, data) {
305 301 that.update_restore_checkpoint(that.notebook.checkpoints);
306 302 });
307 303
308 304 this.events.on('checkpoint_created.Notebook', function (event, data) {
309 305 that.update_restore_checkpoint(that.notebook.checkpoints);
310 306 });
307
308 this.events.on('notebook_loaded.Notebook', function() {
309 var langinfo = that.notebook.metadata.language_info || {};
310 that.update_nbconvert_script(langinfo);
311 });
312
313 this.events.on('kernel_ready.Kernel', function(event, data) {
314 var langinfo = data.kernel.info_reply.language_info || {};
315 that.update_nbconvert_script(langinfo);
316 });
311 317 };
312 318
313 319 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
314 320 var ul = this.element.find("#restore_checkpoint").find("ul");
315 321 ul.empty();
316 322 if (!checkpoints || checkpoints.length === 0) {
317 323 ul.append(
318 324 $("<li/>")
319 325 .addClass("disabled")
320 326 .append(
321 327 $("<a/>")
322 328 .text("No checkpoints")
323 329 )
324 330 );
325 331 return;
326 332 }
327 333
328 334 var that = this;
329 335 checkpoints.map(function (checkpoint) {
330 336 var d = new Date(checkpoint.last_modified);
331 337 ul.append(
332 338 $("<li/>").append(
333 339 $("<a/>")
334 340 .attr("href", "#")
335 341 .text(moment(d).format("LLLL"))
336 342 .click(function () {
337 343 that.notebook.restore_checkpoint_dialog(checkpoint);
338 344 })
339 345 )
340 346 );
341 347 });
342 348 };
349
350 MenuBar.prototype.update_nbconvert_script = function(langinfo) {
351 // Set the 'Download as foo' menu option for the relevant language.
352 var el = this.element.find('#download_script');
353 var that = this;
354
355 // Set menu entry text to e.g. "Python (.py)"
356 var langname = (langinfo.name || 'Script')
357 langname = langname.charAt(0).toUpperCase()+langname.substr(1) // Capitalise
358 el.find('a').text(langname + ' (.'+(langinfo.file_extension || 'txt')+')');
359
360 // Unregister any previously registered handlers
361 el.off('click');
362 if (langinfo.nbconvert_exporter) {
363 // Metadata specifies a specific exporter, e.g. 'python'
364 el.click(function() {
365 that._nbconvert(langinfo.nbconvert_exporter, true);
366 });
367 } else {
368 // Use generic 'script' exporter
369 el.click(function() {
370 that._nbconvert('script', true);
371 });
372 }
373 };
343 374
344 375 // Backwards compatability.
345 376 IPython.MenuBar = MenuBar;
346 377
347 378 return {'MenuBar': MenuBar};
348 379 });
@@ -1,328 +1,328 b''
1 1 {% extends "page.html" %}
2 2
3 3 {% block stylesheet %}
4 4
5 5 {% if mathjax_url %}
6 6 <script type="text/javascript" src="{{mathjax_url}}?config=TeX-AMS_HTML-full&delayStartupUntil=configured" charset="utf-8"></script>
7 7 {% endif %}
8 8 <script type="text/javascript">
9 9 // MathJax disabled, set as null to distingish from *missing* MathJax,
10 10 // where it will be undefined, and should prompt a dialog later.
11 11 window.mathjax_url = "{{mathjax_url}}";
12 12 </script>
13 13
14 14 <link rel="stylesheet" href="{{ static_url("components/bootstrap-tour/build/css/bootstrap-tour.min.css") }}" type="text/css" />
15 15 <link rel="stylesheet" href="{{ static_url("components/codemirror/lib/codemirror.css") }}">
16 16
17 17 {{super()}}
18 18
19 19 <link rel="stylesheet" href="{{ static_url("notebook/css/override.css") }}" type="text/css" />
20 20
21 21 {% endblock %}
22 22
23 23 {% block params %}
24 24
25 25 data-project="{{project}}"
26 26 data-base-url="{{base_url}}"
27 27 data-ws-url="{{ws_url}}"
28 28 data-notebook-name="{{notebook_name}}"
29 29 data-notebook-path="{{notebook_path}}"
30 30 class="notebook_app"
31 31
32 32 {% endblock %}
33 33
34 34
35 35 {% block header %}
36 36
37 37
38 38 <span id="save_widget" class="nav pull-left">
39 39 <span id="notebook_name"></span>
40 40 <span id="checkpoint_status"></span>
41 41 <span id="autosave_status"></span>
42 42 </span>
43 43
44 44 <span id="kernel_selector_widget" class="pull-right dropdown">
45 45 <button class="dropdown-toggle" data-toggle="dropdown" type='button' id="current_kernel_spec">
46 46 <span class='kernel_name'>Python</span>
47 47 <span class="caret"></span>
48 48 </button>
49 49 <ul id="kernel_selector" class="dropdown-menu">
50 50 </ul>
51 51 </span>
52 52
53 53 {% endblock %}
54 54
55 55
56 56 {% block site %}
57 57
58 58 <div id="menubar-container" class="container">
59 59 <div id="menubar">
60 60 <div id="menus" class="navbar navbar-default" role="navigation">
61 61 <div class="container-fluid">
62 62 <button type="button" class="btn btn-default navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
63 63 <i class="fa fa-bars"></i>
64 64 <span class="navbar-text">Menu</span>
65 65 </button>
66 66 <ul class="nav navbar-nav navbar-right">
67 67 <li id="kernel_indicator">
68 68 <i id="kernel_indicator_icon"></i>
69 69 </li>
70 70 <li id="modal_indicator">
71 71 <i id="modal_indicator_icon"></i>
72 72 </li>
73 73 <li id="notification_area"></li>
74 74 </ul>
75 75 <div class="navbar-collapse collapse">
76 76 <ul class="nav navbar-nav">
77 77 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a>
78 78 <ul id="file_menu" class="dropdown-menu">
79 79 <li id="new_notebook"
80 80 title="Make a new notebook (Opens a new window)">
81 81 <a href="#">New</a></li>
82 82 <li id="open_notebook"
83 83 title="Opens a new window with the Dashboard view">
84 84 <a href="#">Open...</a></li>
85 85 <!-- <hr/> -->
86 86 <li class="divider"></li>
87 87 <li id="copy_notebook"
88 88 title="Open a copy of this notebook's contents and start a new kernel">
89 89 <a href="#">Make a Copy...</a></li>
90 90 <li id="rename_notebook"><a href="#">Rename...</a></li>
91 91 <li id="save_checkpoint"><a href="#">Save and Checkpoint</a></li>
92 92 <!-- <hr/> -->
93 93 <li class="divider"></li>
94 94 <li id="restore_checkpoint" class="dropdown-submenu"><a href="#">Revert to Checkpoint</a>
95 95 <ul class="dropdown-menu">
96 96 <li><a href="#"></a></li>
97 97 <li><a href="#"></a></li>
98 98 <li><a href="#"></a></li>
99 99 <li><a href="#"></a></li>
100 100 <li><a href="#"></a></li>
101 101 </ul>
102 102 </li>
103 103 <li class="divider"></li>
104 104 <li id="print_preview"><a href="#">Print Preview</a></li>
105 105 <li class="dropdown-submenu"><a href="#">Download as</a>
106 106 <ul class="dropdown-menu">
107 107 <li id="download_ipynb"><a href="#">IPython Notebook (.ipynb)</a></li>
108 <li id="download_py"><a href="#">Python (.py)</a></li>
108 <li id="download_script"><a href="#">Script</a></li>
109 109 <li id="download_html"><a href="#">HTML (.html)</a></li>
110 110 <li id="download_rst"><a href="#">reST (.rst)</a></li>
111 111 <li id="download_pdf"><a href="#">PDF (.pdf)</a></li>
112 112 </ul>
113 113 </li>
114 114 <li class="divider"></li>
115 115 <li id="trust_notebook"
116 116 title="Trust the output of this notebook">
117 117 <a href="#" >Trust Notebook</a></li>
118 118 <li class="divider"></li>
119 119 <li id="kill_and_exit"
120 120 title="Shutdown this notebook's kernel, and close this window">
121 121 <a href="#" >Close and halt</a></li>
122 122 </ul>
123 123 </li>
124 124 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Edit</a>
125 125 <ul id="edit_menu" class="dropdown-menu">
126 126 <li id="cut_cell"><a href="#">Cut Cell</a></li>
127 127 <li id="copy_cell"><a href="#">Copy Cell</a></li>
128 128 <li id="paste_cell_above" class="disabled"><a href="#">Paste Cell Above</a></li>
129 129 <li id="paste_cell_below" class="disabled"><a href="#">Paste Cell Below</a></li>
130 130 <li id="paste_cell_replace" class="disabled"><a href="#">Paste Cell &amp; Replace</a></li>
131 131 <li id="delete_cell"><a href="#">Delete Cell</a></li>
132 132 <li id="undelete_cell" class="disabled"><a href="#">Undo Delete Cell</a></li>
133 133 <li class="divider"></li>
134 134 <li id="split_cell"><a href="#">Split Cell</a></li>
135 135 <li id="merge_cell_above"><a href="#">Merge Cell Above</a></li>
136 136 <li id="merge_cell_below"><a href="#">Merge Cell Below</a></li>
137 137 <li class="divider"></li>
138 138 <li id="move_cell_up"><a href="#">Move Cell Up</a></li>
139 139 <li id="move_cell_down"><a href="#">Move Cell Down</a></li>
140 140 <li class="divider"></li>
141 141 <li id="edit_nb_metadata"><a href="#">Edit Notebook Metadata</a></li>
142 142 </ul>
143 143 </li>
144 144 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">View</a>
145 145 <ul id="view_menu" class="dropdown-menu">
146 146 <li id="toggle_header"
147 147 title="Show/Hide the IPython Notebook logo and notebook title (above menu bar)">
148 148 <a href="#">Toggle Header</a></li>
149 149 <li id="toggle_toolbar"
150 150 title="Show/Hide the action icons (below menu bar)">
151 151 <a href="#">Toggle Toolbar</a></li>
152 152 </ul>
153 153 </li>
154 154 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Insert</a>
155 155 <ul id="insert_menu" class="dropdown-menu">
156 156 <li id="insert_cell_above"
157 157 title="Insert an empty Code cell above the currently active cell">
158 158 <a href="#">Insert Cell Above</a></li>
159 159 <li id="insert_cell_below"
160 160 title="Insert an empty Code cell below the currently active cell">
161 161 <a href="#">Insert Cell Below</a></li>
162 162 </ul>
163 163 </li>
164 164 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Cell</a>
165 165 <ul id="cell_menu" class="dropdown-menu">
166 166 <li id="run_cell" title="Run this cell, and move cursor to the next one">
167 167 <a href="#">Run</a></li>
168 168 <li id="run_cell_select_below" title="Run this cell, select below">
169 169 <a href="#">Run and Select Below</a></li>
170 170 <li id="run_cell_insert_below" title="Run this cell, insert below">
171 171 <a href="#">Run and Insert Below</a></li>
172 172 <li id="run_all_cells" title="Run all cells in the notebook">
173 173 <a href="#">Run All</a></li>
174 174 <li id="run_all_cells_above" title="Run all cells above (but not including) this cell">
175 175 <a href="#">Run All Above</a></li>
176 176 <li id="run_all_cells_below" title="Run this cell and all cells below it">
177 177 <a href="#">Run All Below</a></li>
178 178 <li class="divider"></li>
179 179 <li id="change_cell_type" class="dropdown-submenu"
180 180 title="All cells in the notebook have a cell type. By default, new cells are created as 'Code' cells">
181 181 <a href="#">Cell Type</a>
182 182 <ul class="dropdown-menu">
183 183 <li id="to_code"
184 184 title="Contents will be sent to the kernel for execution, and output will display in the footer of cell">
185 185 <a href="#">Code</a></li>
186 186 <li id="to_markdown"
187 187 title="Contents will be rendered as HTML and serve as explanatory text">
188 188 <a href="#">Markdown</a></li>
189 189 <li id="to_raw"
190 190 title="Contents will pass through nbconvert unmodified">
191 191 <a href="#">Raw NBConvert</a></li>
192 192 </ul>
193 193 </li>
194 194 <li class="divider"></li>
195 195 <li id="current_outputs" class="dropdown-submenu"><a href="#">Current Output</a>
196 196 <ul class="dropdown-menu">
197 197 <li id="toggle_current_output"
198 198 title="Hide/Show the output of the current cell">
199 199 <a href="#">Toggle</a>
200 200 </li>
201 201 <li id="toggle_current_output_scroll"
202 202 title="Scroll the output of the current cell">
203 203 <a href="#">Toggle Scrolling</a>
204 204 </li>
205 205 <li id="clear_current_output"
206 206 title="Clear the output of the current cell">
207 207 <a href="#">Clear</a>
208 208 </li>
209 209 </ul>
210 210 </li>
211 211 <li id="all_outputs" class="dropdown-submenu"><a href="#">All Output</a>
212 212 <ul class="dropdown-menu">
213 213 <li id="toggle_all_output"
214 214 title="Hide/Show the output of all cells">
215 215 <a href="#">Toggle</a>
216 216 </li>
217 217 <li id="toggle_all_output_scroll"
218 218 title="Scroll the output of all cells">
219 219 <a href="#">Toggle Scrolling</a>
220 220 </li>
221 221 <li id="clear_all_output"
222 222 title="Clear the output of all cells">
223 223 <a href="#">Clear</a>
224 224 </li>
225 225 </ul>
226 226 </li>
227 227 </ul>
228 228 </li>
229 229 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Kernel</a>
230 230 <ul id="kernel_menu" class="dropdown-menu">
231 231 <li id="int_kernel"
232 232 title="Send KeyboardInterrupt (CTRL-C) to the Kernel">
233 233 <a href="#">Interrupt</a>
234 234 </li>
235 235 <li id="restart_kernel"
236 236 title="Restart the Kernel">
237 237 <a href="#">Restart</a>
238 238 </li>
239 239 <li id="reconnect_kernel"
240 240 title="Reconnect to the Kernel">
241 241 <a href="#">Reconnect</a>
242 242 </li>
243 243 <li class="divider"></li>
244 244 <li id="menu-change-kernel" class="dropdown-submenu">
245 245 <a href="#">Change kernel</a>
246 246 <ul class="dropdown-menu" id="menu-change-kernel-submenu"></ul>
247 247 </li>
248 248 </ul>
249 249 </li>
250 250 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Help</a>
251 251 <ul id="help_menu" class="dropdown-menu">
252 252 <li id="notebook_tour" title="A quick tour of the notebook user interface"><a href="#">User Interface Tour</a></li>
253 253 <li id="keyboard_shortcuts" title="Opens a tooltip with all keyboard shortcuts"><a href="#">Keyboard Shortcuts</a></li>
254 254 <li class="divider"></li>
255 255 {% set
256 256 sections = (
257 257 (
258 258 ("http://ipython.org/documentation.html","IPython Help",True),
259 259 ("http://nbviewer.ipython.org/github/ipython/ipython/tree/2.x/examples/Index.ipynb", "Notebook Help", True),
260 260 ),(
261 261 ("http://docs.python.org","Python",True),
262 262 ("http://help.github.com/articles/github-flavored-markdown","Markdown",True),
263 263 ("http://docs.scipy.org/doc/numpy/reference/","NumPy",True),
264 264 ("http://docs.scipy.org/doc/scipy/reference/","SciPy",True),
265 265 ("http://matplotlib.org/contents.html","Matplotlib",True),
266 266 ("http://docs.sympy.org/latest/index.html","SymPy",True),
267 267 ("http://pandas.pydata.org/pandas-docs/stable/","pandas", True)
268 268 )
269 269 )
270 270 %}
271 271
272 272 {% for helplinks in sections %}
273 273 {% for link in helplinks %}
274 274 <li><a href="{{link[0]}}" {{'target="_blank" title="Opens in a new window"' if link[2]}}>
275 275 {{'<i class="fa fa-external-link menu-icon pull-right"></i>' if link[2]}}
276 276 {{link[1]}}
277 277 </a></li>
278 278 {% endfor %}
279 279 {% if not loop.last %}
280 280 <li class="divider"></li>
281 281 {% endif %}
282 282 {% endfor %}
283 283 <li class="divider"></li>
284 284 <li title="About IPython Notebook"><a id="notebook_about" href="#">About</a></li>
285 285 </ul>
286 286 </li>
287 287 </ul>
288 288 </div>
289 289 </div>
290 290 </div>
291 291 </div>
292 292 <div id="maintoolbar" class="navbar">
293 293 <div class="toolbar-inner navbar-inner navbar-nobg">
294 294 <div id="maintoolbar-container" class="container"></div>
295 295 </div>
296 296 </div>
297 297 </div>
298 298
299 299 <div id="ipython-main-app">
300 300
301 301 <div id="notebook_panel">
302 302 <div id="notebook"></div>
303 303 <div id="pager_splitter"></div>
304 304 <div id="pager">
305 305 <div id='pager_button_area'>
306 306 </div>
307 307 <div id="pager-container" class="container"></div>
308 308 </div>
309 309 </div>
310 310
311 311 </div>
312 312 <div id='tooltip' class='ipython_tooltip' style='display:none'></div>
313 313
314 314
315 315 {% endblock %}
316 316
317 317
318 318 {% block script %}
319 319 {{super()}}
320 320 <script type="text/javascript">
321 321 sys_info = {{sys_info}};
322 322 </script>
323 323
324 324 <script src="{{ static_url("components/text-encoding/lib/encoding.js") }}" charset="utf-8"></script>
325 325
326 326 <script src="{{ static_url("notebook/js/main.js") }}" charset="utf-8"></script>
327 327
328 328 {% endblock %}
@@ -1,328 +1,330 b''
1 1 """The IPython kernel implementation"""
2 2
3 3 import getpass
4 4 import sys
5 5 import traceback
6 6
7 7 from IPython.core import release
8 8 from IPython.html.widgets import Widget
9 9 from IPython.utils.py3compat import builtin_mod, PY3
10 10 from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
11 11 from IPython.utils.traitlets import Instance, Type, Any
12 12 from IPython.utils.decorators import undoc
13 13
14 14 from ..comm import CommManager
15 15 from .kernelbase import Kernel as KernelBase
16 16 from .serialize import serialize_object, unpack_apply_message
17 17 from .zmqshell import ZMQInteractiveShell
18 18
19 19 class IPythonKernel(KernelBase):
20 20 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
21 21 shell_class = Type(ZMQInteractiveShell)
22 22
23 23 user_module = Any()
24 24 def _user_module_changed(self, name, old, new):
25 25 if self.shell is not None:
26 26 self.shell.user_module = new
27 27
28 28 user_ns = Instance(dict, args=None, allow_none=True)
29 29 def _user_ns_changed(self, name, old, new):
30 30 if self.shell is not None:
31 31 self.shell.user_ns = new
32 32 self.shell.init_user_ns()
33 33
34 34 # A reference to the Python builtin 'raw_input' function.
35 35 # (i.e., __builtin__.raw_input for Python 2.7, builtins.input for Python 3)
36 36 _sys_raw_input = Any()
37 37 _sys_eval_input = Any()
38 38
39 39 def __init__(self, **kwargs):
40 40 super(IPythonKernel, self).__init__(**kwargs)
41 41
42 42 # Initialize the InteractiveShell subclass
43 43 self.shell = self.shell_class.instance(parent=self,
44 44 profile_dir = self.profile_dir,
45 45 user_module = self.user_module,
46 46 user_ns = self.user_ns,
47 47 kernel = self,
48 48 )
49 49 self.shell.displayhook.session = self.session
50 50 self.shell.displayhook.pub_socket = self.iopub_socket
51 51 self.shell.displayhook.topic = self._topic('execute_result')
52 52 self.shell.display_pub.session = self.session
53 53 self.shell.display_pub.pub_socket = self.iopub_socket
54 54 self.shell.data_pub.session = self.session
55 55 self.shell.data_pub.pub_socket = self.iopub_socket
56 56
57 57 # TMP - hack while developing
58 58 self.shell._reply_content = None
59 59
60 60 self.comm_manager = CommManager(shell=self.shell, parent=self,
61 61 kernel=self)
62 62 self.comm_manager.register_target('ipython.widget', Widget.handle_comm_opened)
63 63
64 64 self.shell.configurables.append(self.comm_manager)
65 65 comm_msg_types = [ 'comm_open', 'comm_msg', 'comm_close' ]
66 66 for msg_type in comm_msg_types:
67 67 self.shell_handlers[msg_type] = getattr(self.comm_manager, msg_type)
68 68
69 69 # Kernel info fields
70 70 implementation = 'ipython'
71 71 implementation_version = release.version
72 72 language = 'python'
73 73 language_version = sys.version.split()[0]
74 74 language_info = {'mimetype': 'text/x-python',
75 75 'codemirror_mode': {'name': 'ipython',
76 76 'version': sys.version_info[0]},
77 77 'pygments_lexer': 'ipython%d' % (3 if PY3 else 2),
78 'nbconvert_exporter': 'python',
79 'file_extension': 'py'
78 80 }
79 81 @property
80 82 def banner(self):
81 83 return self.shell.banner
82 84
83 85 def start(self):
84 86 self.shell.exit_now = False
85 87 super(IPythonKernel, self).start()
86 88
87 89 def set_parent(self, ident, parent):
88 90 """Overridden from parent to tell the display hook and output streams
89 91 about the parent message.
90 92 """
91 93 super(IPythonKernel, self).set_parent(ident, parent)
92 94 self.shell.set_parent(parent)
93 95
94 96 def _forward_input(self, allow_stdin=False):
95 97 """Forward raw_input and getpass to the current frontend.
96 98
97 99 via input_request
98 100 """
99 101 self._allow_stdin = allow_stdin
100 102
101 103 if PY3:
102 104 self._sys_raw_input = builtin_mod.input
103 105 builtin_mod.input = self.raw_input
104 106 else:
105 107 self._sys_raw_input = builtin_mod.raw_input
106 108 self._sys_eval_input = builtin_mod.input
107 109 builtin_mod.raw_input = self.raw_input
108 110 builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
109 111 self._save_getpass = getpass.getpass
110 112 getpass.getpass = self.getpass
111 113
112 114 def _restore_input(self):
113 115 """Restore raw_input, getpass"""
114 116 if PY3:
115 117 builtin_mod.input = self._sys_raw_input
116 118 else:
117 119 builtin_mod.raw_input = self._sys_raw_input
118 120 builtin_mod.input = self._sys_eval_input
119 121
120 122 getpass.getpass = self._save_getpass
121 123
122 124 @property
123 125 def execution_count(self):
124 126 return self.shell.execution_count
125 127
126 128 @execution_count.setter
127 129 def execution_count(self, value):
128 130 # Ignore the incrememnting done by KernelBase, in favour of our shell's
129 131 # execution counter.
130 132 pass
131 133
132 134 def do_execute(self, code, silent, store_history=True,
133 135 user_expressions=None, allow_stdin=False):
134 136 shell = self.shell # we'll need this a lot here
135 137
136 138 self._forward_input(allow_stdin)
137 139
138 140 reply_content = {}
139 141 # FIXME: the shell calls the exception handler itself.
140 142 shell._reply_content = None
141 143 try:
142 144 shell.run_cell(code, store_history=store_history, silent=silent)
143 145 except:
144 146 status = u'error'
145 147 # FIXME: this code right now isn't being used yet by default,
146 148 # because the run_cell() call above directly fires off exception
147 149 # reporting. This code, therefore, is only active in the scenario
148 150 # where runlines itself has an unhandled exception. We need to
149 151 # uniformize this, for all exception construction to come from a
150 152 # single location in the codbase.
151 153 etype, evalue, tb = sys.exc_info()
152 154 tb_list = traceback.format_exception(etype, evalue, tb)
153 155 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
154 156 else:
155 157 status = u'ok'
156 158 finally:
157 159 self._restore_input()
158 160
159 161 reply_content[u'status'] = status
160 162
161 163 # Return the execution counter so clients can display prompts
162 164 reply_content['execution_count'] = shell.execution_count - 1
163 165
164 166 # FIXME - fish exception info out of shell, possibly left there by
165 167 # runlines. We'll need to clean up this logic later.
166 168 if shell._reply_content is not None:
167 169 reply_content.update(shell._reply_content)
168 170 e_info = dict(engine_uuid=self.ident, engine_id=self.int_id, method='execute')
169 171 reply_content['engine_info'] = e_info
170 172 # reset after use
171 173 shell._reply_content = None
172 174
173 175 if 'traceback' in reply_content:
174 176 self.log.info("Exception in execute request:\n%s", '\n'.join(reply_content['traceback']))
175 177
176 178
177 179 # At this point, we can tell whether the main code execution succeeded
178 180 # or not. If it did, we proceed to evaluate user_expressions
179 181 if reply_content['status'] == 'ok':
180 182 reply_content[u'user_expressions'] = \
181 183 shell.user_expressions(user_expressions or {})
182 184 else:
183 185 # If there was an error, don't even try to compute expressions
184 186 reply_content[u'user_expressions'] = {}
185 187
186 188 # Payloads should be retrieved regardless of outcome, so we can both
187 189 # recover partial output (that could have been generated early in a
188 190 # block, before an error) and clear the payload system always.
189 191 reply_content[u'payload'] = shell.payload_manager.read_payload()
190 192 # Be agressive about clearing the payload because we don't want
191 193 # it to sit in memory until the next execute_request comes in.
192 194 shell.payload_manager.clear_payload()
193 195
194 196 return reply_content
195 197
196 198 def do_complete(self, code, cursor_pos):
197 199 # FIXME: IPython completers currently assume single line,
198 200 # but completion messages give multi-line context
199 201 # For now, extract line from cell, based on cursor_pos:
200 202 if cursor_pos is None:
201 203 cursor_pos = len(code)
202 204 line, offset = line_at_cursor(code, cursor_pos)
203 205 line_cursor = cursor_pos - offset
204 206
205 207 txt, matches = self.shell.complete('', line, line_cursor)
206 208 return {'matches' : matches,
207 209 'cursor_end' : cursor_pos,
208 210 'cursor_start' : cursor_pos - len(txt),
209 211 'metadata' : {},
210 212 'status' : 'ok'}
211 213
212 214 def do_inspect(self, code, cursor_pos, detail_level=0):
213 215 name = token_at_cursor(code, cursor_pos)
214 216 info = self.shell.object_inspect(name)
215 217
216 218 reply_content = {'status' : 'ok'}
217 219 reply_content['data'] = data = {}
218 220 reply_content['metadata'] = {}
219 221 reply_content['found'] = info['found']
220 222 if info['found']:
221 223 info_text = self.shell.object_inspect_text(
222 224 name,
223 225 detail_level=detail_level,
224 226 )
225 227 data['text/plain'] = info_text
226 228
227 229 return reply_content
228 230
229 231 def do_history(self, hist_access_type, output, raw, session=None, start=None,
230 232 stop=None, n=None, pattern=None, unique=False):
231 233 if hist_access_type == 'tail':
232 234 hist = self.shell.history_manager.get_tail(n, raw=raw, output=output,
233 235 include_latest=True)
234 236
235 237 elif hist_access_type == 'range':
236 238 hist = self.shell.history_manager.get_range(session, start, stop,
237 239 raw=raw, output=output)
238 240
239 241 elif hist_access_type == 'search':
240 242 hist = self.shell.history_manager.search(
241 243 pattern, raw=raw, output=output, n=n, unique=unique)
242 244 else:
243 245 hist = []
244 246
245 247 return {'history' : list(hist)}
246 248
247 249 def do_shutdown(self, restart):
248 250 self.shell.exit_now = True
249 251 return dict(status='ok', restart=restart)
250 252
251 253 def do_is_complete(self, code):
252 254 status, indent_spaces = self.shell.input_transformer_manager.check_complete(code)
253 255 r = {'status': status}
254 256 if status == 'incomplete':
255 257 r['indent'] = ' ' * indent_spaces
256 258 return r
257 259
258 260 def do_apply(self, content, bufs, msg_id, reply_metadata):
259 261 shell = self.shell
260 262 try:
261 263 working = shell.user_ns
262 264
263 265 prefix = "_"+str(msg_id).replace("-","")+"_"
264 266
265 267 f,args,kwargs = unpack_apply_message(bufs, working, copy=False)
266 268
267 269 fname = getattr(f, '__name__', 'f')
268 270
269 271 fname = prefix+"f"
270 272 argname = prefix+"args"
271 273 kwargname = prefix+"kwargs"
272 274 resultname = prefix+"result"
273 275
274 276 ns = { fname : f, argname : args, kwargname : kwargs , resultname : None }
275 277 # print ns
276 278 working.update(ns)
277 279 code = "%s = %s(*%s,**%s)" % (resultname, fname, argname, kwargname)
278 280 try:
279 281 exec(code, shell.user_global_ns, shell.user_ns)
280 282 result = working.get(resultname)
281 283 finally:
282 284 for key in ns:
283 285 working.pop(key)
284 286
285 287 result_buf = serialize_object(result,
286 288 buffer_threshold=self.session.buffer_threshold,
287 289 item_threshold=self.session.item_threshold,
288 290 )
289 291
290 292 except:
291 293 # invoke IPython traceback formatting
292 294 shell.showtraceback()
293 295 # FIXME - fish exception info out of shell, possibly left there by
294 296 # run_code. We'll need to clean up this logic later.
295 297 reply_content = {}
296 298 if shell._reply_content is not None:
297 299 reply_content.update(shell._reply_content)
298 300 e_info = dict(engine_uuid=self.ident, engine_id=self.int_id, method='apply')
299 301 reply_content['engine_info'] = e_info
300 302 # reset after use
301 303 shell._reply_content = None
302 304
303 305 self.send_response(self.iopub_socket, u'error', reply_content,
304 306 ident=self._topic('error'))
305 307 self.log.info("Exception in apply request:\n%s", '\n'.join(reply_content['traceback']))
306 308 result_buf = []
307 309
308 310 if reply_content['ename'] == 'UnmetDependency':
309 311 reply_metadata['dependencies_met'] = False
310 312 else:
311 313 reply_content = {'status' : 'ok'}
312 314
313 315 return reply_content, result_buf
314 316
315 317 def do_clear(self):
316 318 self.shell.reset(False)
317 319 return dict(status='ok')
318 320
319 321
320 322 # This exists only for backwards compatibility - use IPythonKernel instead
321 323
322 324 @undoc
323 325 class Kernel(IPythonKernel):
324 326 def __init__(self, *args, **kwargs):
325 327 import warnings
326 328 warnings.warn('Kernel is a deprecated alias of IPython.kernel.zmq.ipkernel.IPythonKernel',
327 329 DeprecationWarning)
328 330 super(Kernel, self).__init__(*args, **kwargs) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now