##// END OF EJS Templates
Merge pull request #4533 from ivanov/display-isolated...
Min RK -
r14149:0355d3db merge
parent child Browse files
Show More
@@ -0,0 +1,235
1 // Test opening a rich notebook, saving it, and reopening it again.
2 //
3 //toJSON fromJSON toJSON and do a string comparison
4
5
6 // this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js
7 mime = {
8 "text" : "text/plain",
9 "html" : "text/html",
10 "svg" : "image/svg+xml",
11 "png" : "image/png",
12 "jpeg" : "image/jpeg",
13 "latex" : "text/latex",
14 "json" : "application/json",
15 "javascript" : "application/javascript",
16 };
17
18 var black_dot_jpeg="\"\"\"/9j/4AAQSkZJRgABAQEASABIAAD/2wBDACodICUgGiolIiUvLSoyP2lEPzo6P4FcYUxpmYagnpaG\nk5GovfLNqLPltZGT0v/V5fr/////o8v///////L/////2wBDAS0vLz83P3xERHz/rpOu////////\n////////////////////////////////////////////////////////////wgARCAABAAEDAREA\nAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAABP/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEA\nAhADEAAAARn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAEFAn//xAAUEQEAAAAAAAAAAAAA\nAAAAAAAA/9oACAEDAQE/AX//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/AX//xAAUEAEA\nAAAAAAAAAAAAAAAAAAAA/9oACAEBAAY/An//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/\nIX//2gAMAwEAAgADAAAAEB//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDAQE/EH//xAAUEQEA\nAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/EH//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/\nEH//2Q==\"\"\"";
19 var black_dot_png = '\"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QA\\niAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AA\\nAAACAAHiIbwzAAAAAElFTkSuQmCC\"';
20 var svg = "\"<svg width='1cm' height='1cm' viewBox='0 0 1000 500'><defs><style>rect {fill:red;}; </style></defs><rect id='r1' x='200' y='100' width='600' height='300' /></svg>\"";
21
22 // helper function to ensure that the short_name is found in the toJSON
23 // represetnation, while the original in-memory cell retains its long mimetype
24 // name, and that fromJSON also gets its long mimetype name
25 function assert_has(short_name, json, result, result2) {
26 long_name = mime[short_name];
27 this.test.assertTrue(json[0].hasOwnProperty(short_name),
28 'toJSON() representation uses ' + short_name);
29 this.test.assertTrue(result.hasOwnProperty(long_name),
30 'toJSON() original embeded JSON keeps ' + long_name);
31 this.test.assertTrue(result2.hasOwnProperty(long_name),
32 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name);
33 }
34
35 // helper function for checkout that the first two cells have a particular
36 // output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON
37 // for a set of mimetype keys, using their short names ('javascript', 'text',
38 // 'png', etc).
39 function check_output_area(output_type, keys) {
40 this.wait_for_output(0);
41 json = this.evaluate(function() {
42 var json = IPython.notebook.get_cell(0).output_area.toJSON();
43 // appended cell will initially be empty, lets add it some output
44 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
45 return json;
46 });
47 var result = this.get_output_cell(0);
48 var result2 = this.get_output_cell(1);
49 this.test.assertEquals(result.output_type, output_type,
50 'testing ' + output_type + ' for ' + keys.join(' and '));
51
52 for (var idx in keys) {
53 assert_has.apply(this, [keys[idx], json, result, result2]);
54 }
55 }
56
57
58 // helper function to clear the first two cells, set the text of and execute
59 // the first one
60 function clear_and_execute(that, code) {
61 that.evaluate(function() {
62 IPython.notebook.get_cell(0).clear_output();
63 IPython.notebook.get_cell(1).clear_output();
64 });
65 that.set_cell_text(0, code);
66 that.execute_cell(0);
67 }
68
69 casper.notebook_test(function () {
70 this.evaluate(function () {
71 var cell = IPython.notebook.get_cell(0);
72 // "we have to make messes to find out who we are"
73 cell.set_text([
74 "%%javascript",
75 "IPython.notebook.insert_cell_below('code')"
76 ].join('\n')
77 );
78
79 cell.execute();
80 });
81
82 this.wait_for_output(0);
83
84 this.then(function ( ) {
85 var result = this.get_output_cell(0);
86 var num_cells = this.get_cells_length();
87 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
88 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
89 'testing JS embeded with mime key');
90 });
91
92 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
93
94 this.then(function ( ) {
95 check_output_area.apply(this, ['display_data', ['javascript']]);
96
97 });
98
99 this.then(function() {
100 clear_and_execute(this, '%lsmagic');
101 });
102
103 this.then(function () {
104 check_output_area.apply(this, ['pyout', ['text', 'json']]);
105 });
106
107 this.then(function() {
108 clear_and_execute(this,
109 "x = %lsmagic\nfrom IPython.display import display; display(x)");
110 });
111
112 this.then(function ( ) {
113 check_output_area.apply(this, ['display_data', ['text', 'json']]);
114 });
115
116 this.then(function() {
117 clear_and_execute(this,
118 "from IPython.display import Latex; Latex('$X^2$')");
119 });
120
121 this.then(function ( ) {
122 check_output_area.apply(this, ['pyout', ['text', 'latex']]);
123 });
124
125 this.then(function() {
126 clear_and_execute(this,
127 "from IPython.display import Latex, display; display(Latex('$X^2$'))");
128 });
129
130 this.then(function ( ) {
131 check_output_area.apply(this, ['display_data', ['text', 'latex']]);
132 });
133
134 this.then(function() {
135 clear_and_execute(this,
136 "from IPython.display import HTML; HTML('<b>it works!</b>')");
137 });
138
139 this.then(function ( ) {
140 check_output_area.apply(this, ['pyout', ['text', 'html']]);
141 });
142
143 this.then(function() {
144 clear_and_execute(this,
145 "from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))");
146 });
147
148 this.then(function ( ) {
149 check_output_area.apply(this, ['display_data', ['text', 'html']]);
150 });
151
152
153 this.then(function() {
154 clear_and_execute(this,
155 "from IPython.display import Image; Image(" + black_dot_png + ")");
156 });
157 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
158
159 this.then(function ( ) {
160 check_output_area.apply(this, ['pyout', ['text', 'png']]);
161 });
162
163 this.then(function() {
164 clear_and_execute(this,
165 "from IPython.display import Image, display; display(Image(" + black_dot_png + "))");
166 });
167
168 this.then(function ( ) {
169 check_output_area.apply(this, ['display_data', ['text', 'png']]);
170 });
171
172
173 this.then(function() {
174 clear_and_execute(this,
175 "from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')");
176 });
177
178 this.then(function ( ) {
179 check_output_area.apply(this, ['pyout', ['text', 'jpeg']]);
180 });
181
182 this.then(function() {
183 clear_and_execute(this,
184 "from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))");
185 });
186
187 this.then(function ( ) {
188 check_output_area.apply(this, ['display_data', ['text', 'jpeg']]);
189 });
190
191 this.then(function() {
192 clear_and_execute(this,
193 "from IPython.core.display import SVG; SVG(" + svg + ")");
194 });
195
196 this.then(function ( ) {
197 check_output_area.apply(this, ['pyout', ['text', 'svg']]);
198 });
199
200 this.then(function() {
201 clear_and_execute(this,
202 "from IPython.core.display import SVG, display; display(SVG(" + svg + "))");
203 });
204
205 this.then(function ( ) {
206 check_output_area.apply(this, ['display_data', ['text', 'svg']]);
207 });
208
209 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
210
211 this.then(function() {
212 clear_and_execute(this, [
213 "from IPython.core.formatters import HTMLFormatter",
214 "x = HTMLFormatter()",
215 "x.format_type = 'text/superfancymimetype'",
216 "get_ipython().display_formatter.formatters['text/superfancymimetype'] = x",
217 "from IPython.display import HTML, display",
218 'display(HTML("yo"))',
219 "HTML('hello')"].join('\n')
220 );
221
222 });
223
224 this.then(function ( ) {
225 var long_name = 'text/superfancymimetype';
226 var result = this.get_output_cell(0);
227 this.test.assertTrue(result.hasOwnProperty(long_name),
228 'display_data custom mimetype ' + long_name);
229 var result = this.get_output_cell(0, 1);
230 this.test.assertTrue(result.hasOwnProperty(long_name),
231 'pyout custom mimetype ' + long_name);
232
233 });
234
235 });
@@ -239,56 +239,55 var IPython = (function (IPython) {
239 json.text = content.data;
239 json.text = content.data;
240 json.stream = content.name;
240 json.stream = content.name;
241 } else if (msg_type === "display_data") {
241 } else if (msg_type === "display_data") {
242 json = this.convert_mime_types(json, content.data);
242 json = content.data;
243 json.metadata = this.convert_mime_types({}, content.metadata);
243 json.output_type = msg_type;
244 json.metadata = content.metadata;
244 } else if (msg_type === "pyout") {
245 } else if (msg_type === "pyout") {
246 json = content.data;
247 json.output_type = msg_type;
248 json.metadata = content.metadata;
245 json.prompt_number = content.execution_count;
249 json.prompt_number = content.execution_count;
246 json = this.convert_mime_types(json, content.data);
247 json.metadata = this.convert_mime_types({}, content.metadata);
248 } else if (msg_type === "pyerr") {
250 } else if (msg_type === "pyerr") {
249 json.ename = content.ename;
251 json.ename = content.ename;
250 json.evalue = content.evalue;
252 json.evalue = content.evalue;
251 json.traceback = content.traceback;
253 json.traceback = content.traceback;
252 }
254 }
253 // append with dynamic=true
255 this.append_output(json);
254 this.append_output(json, true);
255 };
256 };
256
257
258 OutputArea.mime_map = {
259 "text/plain" : "text",
260 "text/html" : "html",
261 "image/svg+xml" : "svg",
262 "image/png" : "png",
263 "image/jpeg" : "jpeg",
264 "text/latex" : "latex",
265 "application/json" : "json",
266 "application/javascript" : "javascript",
267 };
268
269 OutputArea.mime_map_r = {
270 "text" : "text/plain",
271 "html" : "text/html",
272 "svg" : "image/svg+xml",
273 "png" : "image/png",
274 "jpeg" : "image/jpeg",
275 "latex" : "text/latex",
276 "json" : "application/json",
277 "javascript" : "application/javascript",
278 };
257
279
258 OutputArea.prototype.convert_mime_types = function (json, data) {
280 OutputArea.prototype.rename_keys = function (data, key_map) {
259 if (data === undefined) {
281 var remapped = {};
260 return json;
282 for (var key in data) {
261 }
283 var new_key = key_map[key] || key;
262 if (data['text/plain'] !== undefined) {
284 remapped[new_key] = data[key];
263 json.text = data['text/plain'];
264 }
265 if (data['text/html'] !== undefined) {
266 json.html = data['text/html'];
267 }
268 if (data['image/svg+xml'] !== undefined) {
269 json.svg = data['image/svg+xml'];
270 }
271 if (data['image/png'] !== undefined) {
272 json.png = data['image/png'];
273 }
285 }
274 if (data['image/jpeg'] !== undefined) {
286 return remapped;
275 json.jpeg = data['image/jpeg'];
276 }
277 if (data['text/latex'] !== undefined) {
278 json.latex = data['text/latex'];
279 }
280 if (data['application/json'] !== undefined) {
281 json.json = data['application/json'];
282 }
283 if (data['application/javascript'] !== undefined) {
284 json.javascript = data['application/javascript'];
285 }
286 return json;
287 };
287 };
288
288
289
289
290 OutputArea.prototype.append_output = function (json) {
290 OutputArea.prototype.append_output = function (json, dynamic) {
291 // If dynamic is true, javascript output will be eval'd.
292 this.expand();
291 this.expand();
293 // Clear the output if clear is queued.
292 // Clear the output if clear is queued.
294 var needs_height_reset = false;
293 var needs_height_reset = false;
@@ -298,11 +297,11 var IPython = (function (IPython) {
298 }
297 }
299
298
300 if (json.output_type === 'pyout') {
299 if (json.output_type === 'pyout') {
301 this.append_pyout(json, dynamic);
300 this.append_pyout(json);
302 } else if (json.output_type === 'pyerr') {
301 } else if (json.output_type === 'pyerr') {
303 this.append_pyerr(json);
302 this.append_pyerr(json);
304 } else if (json.output_type === 'display_data') {
303 } else if (json.output_type === 'display_data') {
305 this.append_display_data(json, dynamic);
304 this.append_display_data(json);
306 } else if (json.output_type === 'stream') {
305 } else if (json.output_type === 'stream') {
307 this.append_stream(json);
306 this.append_stream(json);
308 }
307 }
@@ -328,9 +327,19 var IPython = (function (IPython) {
328 };
327 };
329
328
330
329
331 OutputArea.prototype.create_output_subarea = function(md, classes) {
330 function _get_metadata_key(metadata, key, mime) {
331 var mime_md = metadata[mime];
332 // mime-specific higher priority
333 if (mime_md && mime_md[key] !== undefined) {
334 return mime_md[key];
335 }
336 // fallback on global
337 return metadata[key];
338 }
339
340 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
332 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
341 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
333 if (md['isolated']) {
342 if (_get_metadata_key(md, 'isolated', mime)) {
334 // Create an iframe to isolate the subarea from the rest of the
343 // Create an iframe to isolate the subarea from the rest of the
335 // document
344 // document
336 var iframe = $('<iframe/>').addClass('box-flex1');
345 var iframe = $('<iframe/>').addClass('box-flex1');
@@ -403,16 +412,16 var IPython = (function (IPython) {
403 };
412 };
404
413
405
414
406 OutputArea.prototype.append_pyout = function (json, dynamic) {
415 OutputArea.prototype.append_pyout = function (json) {
407 var n = json.prompt_number || ' ';
416 var n = json.prompt_number || ' ';
408 var toinsert = this.create_output_area();
417 var toinsert = this.create_output_area();
409 if (this.prompt_area) {
418 if (this.prompt_area) {
410 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
419 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
411 }
420 }
412 this.append_mime_type(json, toinsert, dynamic);
421 this.append_mime_type(json, toinsert);
413 this._safe_append(toinsert);
422 this._safe_append(toinsert);
414 // If we just output latex, typeset it.
423 // If we just output latex, typeset it.
415 if ((json.latex !== undefined) || (json.html !== undefined)) {
424 if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) {
416 this.typeset();
425 this.typeset();
417 }
426 }
418 };
427 };
@@ -470,37 +479,36 var IPython = (function (IPython) {
470 };
479 };
471
480
472
481
473 OutputArea.prototype.append_display_data = function (json, dynamic) {
482 OutputArea.prototype.append_display_data = function (json) {
474 var toinsert = this.create_output_area();
483 var toinsert = this.create_output_area();
475 if (this.append_mime_type(json, toinsert, dynamic)) {
484 if (this.append_mime_type(json, toinsert)) {
476 this._safe_append(toinsert);
485 this._safe_append(toinsert);
477 // If we just output latex, typeset it.
486 // If we just output latex, typeset it.
478 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
487 if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) {
479 this.typeset();
488 this.typeset();
480 }
489 }
481 }
490 }
482 };
491 };
483
492
484 OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text'];
493 OutputArea.display_order = [
494 'application/javascript',
495 'text/html',
496 'text/latex',
497 'image/svg+xml',
498 'image/png',
499 'image/jpeg',
500 'text/plain'
501 ];
502
503 OutputArea.prototype.append_mime_type = function (json, element) {
485
504
486 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
505 for (var type_i in OutputArea.display_order) {
487 for(var type_i in OutputArea.display_order){
488 var type = OutputArea.display_order[type_i];
506 var type = OutputArea.display_order[type_i];
489 if(json[type] != undefined ){
507 var append = OutputArea.append_map[type];
490 var md = {};
508 if ((json[type] !== undefined) && append) {
491 if (json.metadata && json.metadata[type]) {
509 var md = json.metadata || {};
492 md = json.metadata[type];
510 append.apply(this, [json[type], md, element]);
493 };
511 return true;
494 if(type == 'javascript'){
495 if (dynamic) {
496 this.append_javascript(json.javascript, md, element, dynamic);
497 return true;
498 }
499 } else {
500 this['append_'+type](json[type], md, element);
501 return true;
502 }
503 return false;
504 }
512 }
505 }
513 }
506 return false;
514 return false;
@@ -508,7 +516,8 var IPython = (function (IPython) {
508
516
509
517
510 OutputArea.prototype.append_html = function (html, md, element) {
518 OutputArea.prototype.append_html = function (html, md, element) {
511 var toinsert = this.create_output_subarea(md, "output_html rendered_html");
519 var type = 'text/html';
520 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
512 IPython.keyboard_manager.register_events(toinsert);
521 IPython.keyboard_manager.register_events(toinsert);
513 toinsert.append(html);
522 toinsert.append(html);
514 element.append(toinsert);
523 element.append(toinsert);
@@ -517,7 +526,8 var IPython = (function (IPython) {
517
526
518 OutputArea.prototype.append_javascript = function (js, md, container) {
527 OutputArea.prototype.append_javascript = function (js, md, container) {
519 // We just eval the JS code, element appears in the local scope.
528 // We just eval the JS code, element appears in the local scope.
520 var element = this.create_output_subarea(md, "output_javascript");
529 var type = 'application/javascript';
530 var element = this.create_output_subarea(md, "output_javascript", type);
521 IPython.keyboard_manager.register_events(element);
531 IPython.keyboard_manager.register_events(element);
522 container.append(element);
532 container.append(element);
523 try {
533 try {
@@ -530,7 +540,8 var IPython = (function (IPython) {
530
540
531
541
532 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
542 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
533 var toinsert = this.create_output_subarea(md, "output_text");
543 var type = 'text/plain';
544 var toinsert = this.create_output_subarea(md, "output_text", type);
534 // escape ANSI & HTML specials in plaintext:
545 // escape ANSI & HTML specials in plaintext:
535 data = utils.fixConsole(data);
546 data = utils.fixConsole(data);
536 data = utils.fixCarriageReturn(data);
547 data = utils.fixCarriageReturn(data);
@@ -544,7 +555,8 var IPython = (function (IPython) {
544
555
545
556
546 OutputArea.prototype.append_svg = function (svg, md, element) {
557 OutputArea.prototype.append_svg = function (svg, md, element) {
547 var toinsert = this.create_output_subarea(md, "output_svg");
558 var type = 'image/svg+xml';
559 var toinsert = this.create_output_subarea(md, "output_svg", type);
548 toinsert.append(svg);
560 toinsert.append(svg);
549 element.append(toinsert);
561 element.append(toinsert);
550 };
562 };
@@ -578,7 +590,8 var IPython = (function (IPython) {
578
590
579
591
580 OutputArea.prototype.append_png = function (png, md, element) {
592 OutputArea.prototype.append_png = function (png, md, element) {
581 var toinsert = this.create_output_subarea(md, "output_png");
593 var type = 'image/png';
594 var toinsert = this.create_output_subarea(md, "output_png", type);
582 var img = $("<img/>");
595 var img = $("<img/>");
583 img[0].setAttribute('src','data:image/png;base64,'+png);
596 img[0].setAttribute('src','data:image/png;base64,'+png);
584 if (md['height']) {
597 if (md['height']) {
@@ -594,7 +607,8 var IPython = (function (IPython) {
594
607
595
608
596 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
609 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
597 var toinsert = this.create_output_subarea(md, "output_jpeg");
610 var type = 'image/jpeg';
611 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
598 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
612 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
599 if (md['height']) {
613 if (md['height']) {
600 img.attr('height', md['height']);
614 img.attr('height', md['height']);
@@ -611,11 +625,23 var IPython = (function (IPython) {
611 OutputArea.prototype.append_latex = function (latex, md, element) {
625 OutputArea.prototype.append_latex = function (latex, md, element) {
612 // This method cannot do the typesetting because the latex first has to
626 // This method cannot do the typesetting because the latex first has to
613 // be on the page.
627 // be on the page.
614 var toinsert = this.create_output_subarea(md, "output_latex");
628 var type = 'text/latex';
629 var toinsert = this.create_output_subarea(md, "output_latex", type);
615 toinsert.append(latex);
630 toinsert.append(latex);
616 element.append(toinsert);
631 element.append(toinsert);
617 };
632 };
618
633
634 OutputArea.append_map = {
635 "text/plain" : OutputArea.prototype.append_text,
636 "text/html" : OutputArea.prototype.append_html,
637 "image/svg+xml" : OutputArea.prototype.append_svg,
638 "image/png" : OutputArea.prototype.append_png,
639 "image/jpeg" : OutputArea.prototype.append_jpeg,
640 "text/latex" : OutputArea.prototype.append_latex,
641 "application/json" : OutputArea.prototype.append_json,
642 "application/javascript" : OutputArea.prototype.append_javascript,
643 };
644
619 OutputArea.prototype.append_raw_input = function (msg) {
645 OutputArea.prototype.append_raw_input = function (msg) {
620 var that = this;
646 var that = this;
621 this.expand();
647 this.expand();
@@ -715,18 +741,46 var IPython = (function (IPython) {
715
741
716 OutputArea.prototype.fromJSON = function (outputs) {
742 OutputArea.prototype.fromJSON = function (outputs) {
717 var len = outputs.length;
743 var len = outputs.length;
744 var data;
745
746 // We don't want to display javascript on load, so remove it from the
747 // display order for the duration of this function call, but be sure to
748 // put it back in there so incoming messages that contain javascript
749 // representations get displayed
750 var js_index = OutputArea.display_order.indexOf('application/javascript');
751 OutputArea.display_order.splice(js_index, 1);
752
718 for (var i=0; i<len; i++) {
753 for (var i=0; i<len; i++) {
719 // append with dynamic=false.
754 data = outputs[i];
720 this.append_output(outputs[i], false);
755 var msg_type = data.output_type;
756 if (msg_type === "display_data" || msg_type === "pyout") {
757 // convert short keys to mime keys
758 // TODO: remove mapping of short keys when we update to nbformat 4
759 data = this.rename_keys(data, OutputArea.mime_map_r);
760 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r);
761 }
762
763 this.append_output(data);
721 }
764 }
765
766 // reinsert javascript into display order, see note above
767 OutputArea.display_order.splice(js_index, 0, 'application/javascript');
722 };
768 };
723
769
724
770
725 OutputArea.prototype.toJSON = function () {
771 OutputArea.prototype.toJSON = function () {
726 var outputs = [];
772 var outputs = [];
727 var len = this.outputs.length;
773 var len = this.outputs.length;
774 var data;
728 for (var i=0; i<len; i++) {
775 for (var i=0; i<len; i++) {
729 outputs[i] = this.outputs[i];
776 data = this.outputs[i];
777 var msg_type = data.output_type;
778 if (msg_type === "display_data" || msg_type === "pyout") {
779 // convert mime keys to short keys
780 data = this.rename_keys(data, OutputArea.mime_map);
781 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map);
782 }
783 outputs[i] = data;
730 }
784 }
731 return outputs;
785 return outputs;
732 };
786 };
@@ -1,5 +1,5
1 //
1 //
2 // Test svg isolation
2 // Test display isolation
3 // An object whose metadata contains an "isolated" tag must be isolated
3 // An object whose metadata contains an "isolated" tag must be isolated
4 // from the rest of the document. In the case of inline SVGs, this means
4 // from the rest of the document. In the case of inline SVGs, this means
5 // that multiple SVGs have different scopes. This test checks that there
5 // that multiple SVGs have different scopes. This test checks that there
@@ -21,10 +21,65 casper.notebook_test(function () {
21 + "display_svg(SVG(s2), metadata=dict(isolated=True))\n"
21 + "display_svg(SVG(s2), metadata=dict(isolated=True))\n"
22 );
22 );
23 cell.execute();
23 cell.execute();
24 console.log("hello" );
25 });
26
27 this.then(function() {
28 var fname=this.test.currentTestFile.split('/').pop().toLowerCase();
29 this.echo(fname)
30 this.echo(this.currentUrl)
31 this.evaluate(function (n) {
32 IPython.notebook.rename(n);
33 console.write("hello" + n);
34 IPython.notebook.save_notebook();
35 }, {n : fname});
36 this.echo(this.currentUrl)
37 });
38
39 this.then(function() {
40
41 url = this.evaluate(function() {
42 IPython.notebook.rename("foo");
43 //$("span#notebook_name")[0].click();
44 //$("input")[0].value = "please-work";
45 //$(".btn-primary")[0].click();
46 return document.location.href;
47 })
48 this.echo("renamed" + url);
49 this.echo(this.currentUrl);
50 });
51
52 this.wait_for_output(0);
53
54 this.then(function () {
55 var colors = this.evaluate(function () {
56 var colors = [];
57 var ifr = __utils__.findAll("iframe");
58 var svg1 = ifr[0].contentWindow.document.getElementById('r1');
59 colors[0] = window.getComputedStyle(svg1)["fill"];
60 var svg2 = ifr[1].contentWindow.document.getElementById('r2');
61 colors[1] = window.getComputedStyle(svg2)["fill"];
62 return colors;
63 });
64
65 this.test.assertEquals(colors && colors[0], '#ff0000', 'display_svg() First svg should be red');
66 this.test.assertEquals(colors && colors[1], '#000000', 'display_svg() Second svg should be black');
67 });
68
69 // now ensure that we can pass the same metadata dict to plain old display()
70 this.thenEvaluate(function () {
71 var cell = IPython.notebook.get_cell(0);
72 cell.clear_output();
73 cell.set_text( "from IPython.display import display\n"
74 + "display(SVG(s1), metadata=dict(isolated=True))\n"
75 + "display(SVG(s2), metadata=dict(isolated=True))\n"
76 );
77 cell.execute();
24 });
78 });
25
79
26 this.wait_for_output(0);
80 this.wait_for_output(0);
27
81
82 // same test as original
28 this.then(function () {
83 this.then(function () {
29 var colors = this.evaluate(function () {
84 var colors = this.evaluate(function () {
30 var colors = [];
85 var colors = [];
@@ -36,7 +91,7 casper.notebook_test(function () {
36 return colors;
91 return colors;
37 });
92 });
38
93
39 this.test.assertEquals(colors[0], '#ff0000', 'First svg should be red');
94 this.test.assertEquals(colors && colors[0], '#ff0000', 'display() First svg should be red');
40 this.test.assertEquals(colors[1], '#000000', 'Second svg should be black');
95 this.test.assertEquals(colors && colors[1], '#000000', 'display() Second svg should be black');
41 });
96 });
42 });
97 });
@@ -76,12 +76,13 casper.wait_for_output = function (cell_num) {
76 };
76 };
77
77
78 // return the output of a given cell
78 // return the output of a given cell
79 casper.get_output_cell = function (cell_num) {
79 casper.get_output_cell = function (cell_num, out_num) {
80 var result = casper.evaluate(function (c) {
80 out_num = out_num || 0;
81 var result = casper.evaluate(function (c, o) {
81 var cell = IPython.notebook.get_cell(c);
82 var cell = IPython.notebook.get_cell(c);
82 return cell.output_area.outputs[0];
83 return cell.output_area.outputs[o];
83 },
84 },
84 {c : cell_num});
85 {c : cell_num, o : out_num});
85 return result;
86 return result;
86 };
87 };
87
88
General Comments 0
You need to be logged in to leave comments. Login now