##// END OF EJS Templates
Merge pull request #6069 from minrk/coalesce_streams...
Thomas Kluyver -
r17311:52e1e06a merge
parent child Browse files
Show More
@@ -0,0 +1,98 b''
1 //
2 // Various output tests
3 //
4
5 casper.notebook_test(function () {
6
7 this.test_coalesced_output = function (msg, code, expected) {
8 this.then(function () {
9 this.echo("Test coalesced output: " + msg);
10 });
11
12 this.thenEvaluate(function (code) {
13 IPython.notebook.insert_cell_at_index(0, "code");
14 var cell = IPython.notebook.get_cell(0);
15 cell.set_text(code);
16 cell.execute();
17 }, {code: code});
18
19 this.wait_for_output(0);
20
21 this.then(function () {
22 var results = this.evaluate(function () {
23 var cell = IPython.notebook.get_cell(0);
24 return cell.output_area.outputs;
25 });
26 this.test.assertEquals(results.length, expected.length, "correct number of outputs");
27 for (var i = 0; i < results.length; i++) {
28 var r = results[i];
29 var ex = expected[i];
30 this.test.assertEquals(r.output_type, ex.output_type, "output " + i);
31 if (r.output_type === 'stream') {
32 this.test.assertEquals(r.stream, ex.stream, "stream " + i);
33 this.test.assertEquals(r.text, ex.text, "content " + i);
34 }
35 }
36 });
37
38 };
39
40 this.thenEvaluate(function () {
41 IPython.notebook.insert_cell_at_index(0, "code");
42 var cell = IPython.notebook.get_cell(0);
43 cell.set_text([
44 "from __future__ import print_function",
45 "import sys",
46 "from IPython.display import display"
47 ].join("\n")
48 );
49 cell.execute();
50 });
51
52 this.test_coalesced_output("stdout", [
53 "print(1)",
54 "sys.stdout.flush()",
55 "print(2)",
56 "sys.stdout.flush()",
57 "print(3)"
58 ].join("\n"), [{
59 output_type: "stream",
60 stream: "stdout",
61 text: "1\n2\n3\n"
62 }]
63 );
64
65 this.test_coalesced_output("stdout+sdterr", [
66 "print(1)",
67 "sys.stdout.flush()",
68 "print(2)",
69 "print(3, file=sys.stderr)"
70 ].join("\n"), [{
71 output_type: "stream",
72 stream: "stdout",
73 text: "1\n2\n"
74 },{
75 output_type: "stream",
76 stream: "stderr",
77 text: "3\n"
78 }]
79 );
80
81 this.test_coalesced_output("display splits streams", [
82 "print(1)",
83 "sys.stdout.flush()",
84 "display(2)",
85 "print(3)"
86 ].join("\n"), [{
87 output_type: "stream",
88 stream: "stdout",
89 text: "1\n"
90 },{
91 output_type: "display_data",
92 },{
93 output_type: "stream",
94 stream: "stdout",
95 text: "3\n"
96 }]
97 );
98 });
@@ -0,0 +1,5 b''
1 - Consecutive stream (stdout/stderr) output is merged into a single output
2 in the notebook document.
3 Previously, all output messages were preserved as separate output fields in the JSON.
4 Now, the same merge is applied to the stored output as the displayed output,
5 improving document load time for notebooks with many small outputs.
@@ -280,12 +280,15 b' define(['
280 needs_height_reset = true;
280 needs_height_reset = true;
281 }
281 }
282
282
283 var record_output = true;
284
283 if (json.output_type === 'execute_result') {
285 if (json.output_type === 'execute_result') {
284 this.append_execute_result(json);
286 this.append_execute_result(json);
285 } else if (json.output_type === 'error') {
287 } else if (json.output_type === 'error') {
286 this.append_error(json);
288 this.append_error(json);
287 } else if (json.output_type === 'stream') {
289 } else if (json.output_type === 'stream') {
288 this.append_stream(json);
290 // append_stream might have merged the output with earlier stream output
291 record_output = this.append_stream(json);
289 }
292 }
290
293
291 // We must release the animation fixed height in a callback since Gecko
294 // We must release the animation fixed height in a callback since Gecko
@@ -306,7 +309,9 b' define(['
306 handle_appended();
309 handle_appended();
307 }
310 }
308
311
309 this.outputs.push(json);
312 if (record_output) {
313 this.outputs.push(json);
314 }
310 };
315 };
311
316
312
317
@@ -457,20 +462,23 b' define(['
457 // latest output was in the same stream,
462 // latest output was in the same stream,
458 // so append directly into its pre tag
463 // so append directly into its pre tag
459 // escape ANSI & HTML specials:
464 // escape ANSI & HTML specials:
465 last.text = utils.fixCarriageReturn(last.text + json.text);
460 var pre = this.element.find('div.'+subclass).last().find('pre');
466 var pre = this.element.find('div.'+subclass).last().find('pre');
461 var html = utils.fixCarriageReturn(
467 var html = utils.fixConsole(last.text);
462 pre.html() + utils.fixConsole(text));
463 // The only user content injected with this HTML call is
468 // The only user content injected with this HTML call is
464 // escaped by the fixConsole() method.
469 // escaped by the fixConsole() method.
465 pre.html(html);
470 pre.html(html);
466 return;
471 // return false signals that we merged this output with the previous one,
472 // and the new output shouldn't be recorded.
473 return false;
467 }
474 }
468 }
475 }
469
476
470 if (!text.replace("\r", "")) {
477 if (!text.replace("\r", "")) {
471 // text is nothing (empty string, \r, etc.)
478 // text is nothing (empty string, \r, etc.)
472 // so don't append any elements, which might add undesirable space
479 // so don't append any elements, which might add undesirable space
473 return;
480 // return true to indicate the output should be recorded.
481 return true;
474 }
482 }
475
483
476 // If we got here, attach a new div
484 // If we got here, attach a new div
@@ -480,6 +488,7 b' define(['
480 append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass);
488 append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass);
481 }
489 }
482 this._safe_append(toinsert);
490 this._safe_append(toinsert);
491 return true;
483 };
492 };
484
493
485
494
@@ -151,7 +151,7 b' casper.notebook_test(function () {'
151 'display(textbox)\n' +
151 'display(textbox)\n' +
152 'textbox.add_class("my-throttle-textbox")\n' +
152 'textbox.add_class("my-throttle-textbox")\n' +
153 'def handle_change(name, old, new):\n' +
153 'def handle_change(name, old, new):\n' +
154 ' print(len(new))\n' +
154 ' display(len(new))\n' +
155 ' time.sleep(0.5)\n' +
155 ' time.sleep(0.5)\n' +
156 'textbox.on_trait_change(handle_change, "value")\n' +
156 'textbox.on_trait_change(handle_change, "value")\n' +
157 'print(textbox.model_id)');
157 'print(textbox.model_id)');
@@ -182,7 +182,7 b' casper.notebook_test(function () {'
182 this.test.assert(outputs.length <= 5, 'Messages throttled.');
182 this.test.assert(outputs.length <= 5, 'Messages throttled.');
183
183
184 // We also need to verify that the last state sent was correct.
184 // We also need to verify that the last state sent was correct.
185 var last_state = outputs[outputs.length-1].text;
185 var last_state = outputs[outputs.length-1]['text/plain'];
186 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
186 this.test.assertEquals(last_state, "20", "Last state sent when throttling.");
187 });
187 });
188 });
188 });
@@ -8,14 +8,14 b' casper.notebook_test(function () {'
8
8
9 var button_index = this.append_cell(
9 var button_index = this.append_cell(
10 'button = widgets.ButtonWidget(description="Title")\n' +
10 'button = widgets.ButtonWidget(description="Title")\n' +
11 'display(button)\n'+
11 'display(button)\n' +
12 'print("Success")\n' +
12 'print("Success")\n' +
13 'def handle_click(sender):\n' +
13 'def handle_click(sender):\n' +
14 ' print("Clicked")\n' +
14 ' display("Clicked")\n' +
15 'button.on_click(handle_click)');
15 'button.on_click(handle_click)');
16 this.execute_cell_then(button_index, function(index){
16 this.execute_cell_then(button_index, function(index){
17
17
18 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
18 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
19 'Create button cell executed with correct output.');
19 'Create button cell executed with correct output.');
20
20
21 this.test.assert(this.cell_element_exists(index,
21 this.test.assert(this.cell_element_exists(index,
@@ -37,7 +37,7 b' casper.notebook_test(function () {'
37 this.wait_for_output(button_index, 1);
37 this.wait_for_output(button_index, 1);
38
38
39 this.then(function () {
39 this.then(function () {
40 this.test.assertEquals(this.get_output_cell(button_index, 1).text, 'Clicked\n',
40 this.test.assertEquals(this.get_output_cell(button_index, 1)['text/plain'], "'Clicked'",
41 'Button click event fires.');
41 'Button click event fires.');
42 });
42 });
43 }); No newline at end of file
43 });
General Comments 0
You need to be logged in to leave comments. Login now