##// END OF EJS Templates
Merge pull request #7118 from minrk/script-dispatch...
Thomas Kluyver -
r19278:1b552dcd merge
parent child Browse files
Show More
@@ -1,396 +1,386 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 /**
17 17 * Constructor
18 18 *
19 19 * A MenuBar Class to generate the menubar of IPython notebook
20 20 *
21 21 * Parameters:
22 22 * selector: string
23 23 * options: dictionary
24 24 * Dictionary of keyword arguments.
25 25 * notebook: Notebook instance
26 26 * contents: ContentManager 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 */
34 34 options = options || {};
35 35 this.base_url = options.base_url || utils.get_body_data("baseUrl");
36 36 this.selector = selector;
37 37 this.notebook = options.notebook;
38 38 this.contents = options.contents;
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 var url = utils.url_join_encode(
73 73 this.base_url,
74 74 'nbconvert',
75 75 format,
76 76 notebook_path
77 77 ) + "?download=" + download.toString();
78 78
79 79 var w = window.open()
80 80 if (this.notebook.dirty) {
81 81 this.notebook.save_notebook().then(function() {
82 82 w.location = url;
83 83 });
84 84 } else {
85 85 w.location = url;
86 86 }
87 87 };
88 88
89 89 MenuBar.prototype._size_header = function() {
90 90 /**
91 91 * Update header spacer size.
92 92 */
93 93 this.events.trigger('resize-header.Page');
94 94 };
95 95
96 96 MenuBar.prototype.bind_events = function () {
97 97 /**
98 98 * File
99 99 */
100 100 var that = this;
101 101 this.element.find('#new_notebook').click(function () {
102 102 var w = window.open();
103 103 // Create a new notebook in the same path as the current
104 104 // notebook's path.
105 105 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
106 106 that.contents.new_untitled(parent, {type: "notebook"}).then(
107 107 function (data) {
108 108 w.location = utils.url_join_encode(
109 109 that.base_url, 'notebooks', data.path
110 110 );
111 111 },
112 112 function(error) {
113 113 w.close();
114 114 dialog.modal({
115 115 title : 'Creating Notebook Failed',
116 116 body : "The error was: " + error.message,
117 117 buttons : {'OK' : {'class' : 'btn-primary'}}
118 118 });
119 119 }
120 120 );
121 121 });
122 122 this.element.find('#open_notebook').click(function () {
123 123 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
124 124 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
125 125 });
126 126 this.element.find('#copy_notebook').click(function () {
127 127 that.notebook.copy_notebook();
128 128 return false;
129 129 });
130 130 this.element.find('#download_ipynb').click(function () {
131 131 var base_url = that.notebook.base_url;
132 132 var notebook_path = that.notebook.notebook_path;
133 133 if (that.notebook.dirty) {
134 134 that.notebook.save_notebook({async : false});
135 135 }
136 136
137 137 var url = utils.url_join_encode(base_url, 'files', notebook_path);
138 138 window.open(url + '?download=1');
139 139 });
140 140
141 141 this.element.find('#print_preview').click(function () {
142 142 that._nbconvert('html', false);
143 143 });
144 144
145 145 this.element.find('#download_html').click(function () {
146 146 that._nbconvert('html', true);
147 147 });
148 148
149 149 this.element.find('#download_rst').click(function () {
150 150 that._nbconvert('rst', true);
151 151 });
152 152
153 153 this.element.find('#download_pdf').click(function () {
154 154 that._nbconvert('pdf', true);
155 155 });
156 156
157 this.element.find('#download_script').click(function () {
158 that._nbconvert('script', true);
159 });
160
157 161 this.element.find('#rename_notebook').click(function () {
158 162 that.save_widget.rename_notebook({notebook: that.notebook});
159 163 });
160 164 this.element.find('#save_checkpoint').click(function () {
161 165 that.notebook.save_checkpoint();
162 166 });
163 167 this.element.find('#restore_checkpoint').click(function () {
164 168 });
165 169 this.element.find('#trust_notebook').click(function () {
166 170 that.notebook.trust_notebook();
167 171 });
168 172 this.events.on('trust_changed.Notebook', function (event, trusted) {
169 173 if (trusted) {
170 174 that.element.find('#trust_notebook')
171 175 .addClass("disabled")
172 176 .find("a").text("Trusted Notebook");
173 177 } else {
174 178 that.element.find('#trust_notebook')
175 179 .removeClass("disabled")
176 180 .find("a").text("Trust Notebook");
177 181 }
178 182 });
179 183 this.element.find('#kill_and_exit').click(function () {
180 184 var close_window = function () {
181 185 /**
182 186 * allow closing of new tabs in Chromium, impossible in FF
183 187 */
184 188 window.open('', '_self', '');
185 189 window.close();
186 190 };
187 191 // finish with close on success or failure
188 192 that.notebook.session.delete(close_window, close_window);
189 193 });
190 194 // Edit
191 195 this.element.find('#cut_cell').click(function () {
192 196 that.notebook.cut_cell();
193 197 });
194 198 this.element.find('#copy_cell').click(function () {
195 199 that.notebook.copy_cell();
196 200 });
197 201 this.element.find('#delete_cell').click(function () {
198 202 that.notebook.delete_cell();
199 203 });
200 204 this.element.find('#undelete_cell').click(function () {
201 205 that.notebook.undelete_cell();
202 206 });
203 207 this.element.find('#split_cell').click(function () {
204 208 that.notebook.split_cell();
205 209 });
206 210 this.element.find('#merge_cell_above').click(function () {
207 211 that.notebook.merge_cell_above();
208 212 });
209 213 this.element.find('#merge_cell_below').click(function () {
210 214 that.notebook.merge_cell_below();
211 215 });
212 216 this.element.find('#move_cell_up').click(function () {
213 217 that.notebook.move_cell_up();
214 218 });
215 219 this.element.find('#move_cell_down').click(function () {
216 220 that.notebook.move_cell_down();
217 221 });
218 222 this.element.find('#edit_nb_metadata').click(function () {
219 223 that.notebook.edit_metadata({
220 224 notebook: that.notebook,
221 225 keyboard_manager: that.notebook.keyboard_manager});
222 226 });
223 227
224 228 // View
225 229 this.element.find('#toggle_header').click(function () {
226 230 $('div#header-container').toggle();
227 231 that._size_header();
228 232 });
229 233 this.element.find('#toggle_toolbar').click(function () {
230 234 $('div#maintoolbar').toggle();
231 235 that._size_header();
232 236 });
233 237 // Insert
234 238 this.element.find('#insert_cell_above').click(function () {
235 239 that.notebook.insert_cell_above('code');
236 240 that.notebook.select_prev();
237 241 });
238 242 this.element.find('#insert_cell_below').click(function () {
239 243 that.notebook.insert_cell_below('code');
240 244 that.notebook.select_next();
241 245 });
242 246 // Cell
243 247 this.element.find('#run_cell').click(function () {
244 248 that.notebook.execute_cell();
245 249 });
246 250 this.element.find('#run_cell_select_below').click(function () {
247 251 that.notebook.execute_cell_and_select_below();
248 252 });
249 253 this.element.find('#run_cell_insert_below').click(function () {
250 254 that.notebook.execute_cell_and_insert_below();
251 255 });
252 256 this.element.find('#run_all_cells').click(function () {
253 257 that.notebook.execute_all_cells();
254 258 });
255 259 this.element.find('#run_all_cells_above').click(function () {
256 260 that.notebook.execute_cells_above();
257 261 });
258 262 this.element.find('#run_all_cells_below').click(function () {
259 263 that.notebook.execute_cells_below();
260 264 });
261 265 this.element.find('#to_code').click(function () {
262 266 that.notebook.to_code();
263 267 });
264 268 this.element.find('#to_markdown').click(function () {
265 269 that.notebook.to_markdown();
266 270 });
267 271 this.element.find('#to_raw').click(function () {
268 272 that.notebook.to_raw();
269 273 });
270 274
271 275 this.element.find('#toggle_current_output').click(function () {
272 276 that.notebook.toggle_output();
273 277 });
274 278 this.element.find('#toggle_current_output_scroll').click(function () {
275 279 that.notebook.toggle_output_scroll();
276 280 });
277 281 this.element.find('#clear_current_output').click(function () {
278 282 that.notebook.clear_output();
279 283 });
280 284
281 285 this.element.find('#toggle_all_output').click(function () {
282 286 that.notebook.toggle_all_output();
283 287 });
284 288 this.element.find('#toggle_all_output_scroll').click(function () {
285 289 that.notebook.toggle_all_output_scroll();
286 290 });
287 291 this.element.find('#clear_all_output').click(function () {
288 292 that.notebook.clear_all_output();
289 293 });
290 294
291 295 // Kernel
292 296 this.element.find('#int_kernel').click(function () {
293 297 that.notebook.kernel.interrupt();
294 298 });
295 299 this.element.find('#restart_kernel').click(function () {
296 300 that.notebook.restart_kernel();
297 301 });
298 302 this.element.find('#reconnect_kernel').click(function () {
299 303 that.notebook.kernel.reconnect();
300 304 });
301 305 // Help
302 306 if (this.tour) {
303 307 this.element.find('#notebook_tour').click(function () {
304 308 that.tour.start();
305 309 });
306 310 } else {
307 311 this.element.find('#notebook_tour').addClass("disabled");
308 312 }
309 313 this.element.find('#keyboard_shortcuts').click(function () {
310 314 that.quick_help.show_keyboard_shortcuts();
311 315 });
312 316
313 317 this.update_restore_checkpoint(null);
314 318
315 319 this.events.on('checkpoints_listed.Notebook', function (event, data) {
316 320 that.update_restore_checkpoint(that.notebook.checkpoints);
317 321 });
318 322
319 323 this.events.on('checkpoint_created.Notebook', function (event, data) {
320 324 that.update_restore_checkpoint(that.notebook.checkpoints);
321 325 });
322 326
323 327 this.events.on('notebook_loaded.Notebook', function() {
324 328 var langinfo = that.notebook.metadata.language_info || {};
325 329 that.update_nbconvert_script(langinfo);
326 330 });
327 331
328 332 this.events.on('kernel_ready.Kernel', function(event, data) {
329 333 var langinfo = data.kernel.info_reply.language_info || {};
330 334 that.update_nbconvert_script(langinfo);
331 335 });
332 336 };
333 337
334 338 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
335 339 var ul = this.element.find("#restore_checkpoint").find("ul");
336 340 ul.empty();
337 341 if (!checkpoints || checkpoints.length === 0) {
338 342 ul.append(
339 343 $("<li/>")
340 344 .addClass("disabled")
341 345 .append(
342 346 $("<a/>")
343 347 .text("No checkpoints")
344 348 )
345 349 );
346 350 return;
347 351 }
348 352
349 353 var that = this;
350 354 checkpoints.map(function (checkpoint) {
351 355 var d = new Date(checkpoint.last_modified);
352 356 ul.append(
353 357 $("<li/>").append(
354 358 $("<a/>")
355 359 .attr("href", "#")
356 360 .text(moment(d).format("LLLL"))
357 361 .click(function () {
358 362 that.notebook.restore_checkpoint_dialog(checkpoint);
359 363 })
360 364 )
361 365 );
362 366 });
363 367 };
364 368
365 369 MenuBar.prototype.update_nbconvert_script = function(langinfo) {
366 370 /**
367 371 * Set the 'Download as foo' menu option for the relevant language.
368 372 */
369 373 var el = this.element.find('#download_script');
370 374 var that = this;
371 375
372 376 // Set menu entry text to e.g. "Python (.py)"
373 377 var langname = (langinfo.name || 'Script')
374 378 langname = langname.charAt(0).toUpperCase()+langname.substr(1) // Capitalise
375 379 el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
376
377 // Unregister any previously registered handlers
378 el.off('click');
379 if (langinfo.nbconvert_exporter) {
380 // Metadata specifies a specific exporter, e.g. 'python'
381 el.click(function() {
382 that._nbconvert(langinfo.nbconvert_exporter, true);
383 });
384 } else {
385 // Use generic 'script' exporter
386 el.click(function() {
387 that._nbconvert('script', true);
388 });
389 }
390 380 };
391 381
392 382 // Backwards compatability.
393 383 IPython.MenuBar = MenuBar;
394 384
395 385 return {'MenuBar': MenuBar};
396 386 });
@@ -1,14 +1,33 b''
1 1 """Generic script exporter class for any kernel language"""
2 2
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
5
3 6 from .templateexporter import TemplateExporter
4 7
8 from IPython.utils.traitlets import Dict
9
5 10 class ScriptExporter(TemplateExporter):
11
12 _exporters = Dict()
13
6 14 def _template_file_default(self):
7 15 return 'script'
8 16
9 17 def from_notebook_node(self, nb, resources=None, **kw):
10 18 langinfo = nb.metadata.get('language_info', {})
19
20 # delegate to custom exporter, if specified
21 exporter_name = langinfo.get('nbconvert_exporter')
22 if exporter_name and exporter_name != 'script':
23 self.log.debug("Loading script exporter: %s", exporter_name)
24 from .export import exporter_map
25 if exporter_name not in self._exporters:
26 Exporter = exporter_map[exporter_name]
27 self._exporters[exporter_name] = Exporter(parent=self)
28 exporter = self._exporters[exporter_name]
29 return exporter.from_notebook_node(nb, resources, **kw)
30
11 31 self.file_extension = langinfo.get('file_extension', '.txt')
12 32 self.output_mimetype = langinfo.get('mimetype', 'text/plain')
13
14 33 return super(ScriptExporter, self).from_notebook_node(nb, resources, **kw)
General Comments 0
You need to be logged in to leave comments. Login now