nb_roundtrip.js
246 lines
| 8.8 KiB
| application/javascript
|
JavascriptLexer
Paul Ivanov
|
r14125 | // Test opening a rich notebook, saving it, and reopening it again. | ||
// | ||||
//toJSON fromJSON toJSON and do a string comparison | ||||
Paul Ivanov
|
r14137 | |||
// this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js | ||||
mime = { | ||||
"text" : "text/plain", | ||||
"html" : "text/html", | ||||
"svg" : "image/svg+xml", | ||||
"png" : "image/png", | ||||
"jpeg" : "image/jpeg", | ||||
"latex" : "text/latex", | ||||
"json" : "application/json", | ||||
"javascript" : "application/javascript", | ||||
}; | ||||
MinRK
|
r14163 | var black_dot_jpeg="u\"\"\"/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==\"\"\""; | ||
var black_dot_png = 'u\"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QA\\niAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AA\\nAAACAAHiIbwzAAAAAElFTkSuQmCC\"'; | ||||
Paul Ivanov
|
r14147 | 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>\""; | ||
Paul Ivanov
|
r14145 | |||
Paul Ivanov
|
r14139 | // helper function to ensure that the short_name is found in the toJSON | ||
// represetnation, while the original in-memory cell retains its long mimetype | ||||
// name, and that fromJSON also gets its long mimetype name | ||||
Paul Ivanov
|
r14137 | function assert_has(short_name, json, result, result2) { | ||
long_name = mime[short_name]; | ||||
this.test.assertTrue(json[0].hasOwnProperty(short_name), | ||||
'toJSON() representation uses ' + short_name); | ||||
Paul Ivanov
|
r14138 | this.test.assertTrue(result.hasOwnProperty(long_name), | ||
MinRK
|
r14905 | 'toJSON() original embedded JSON keeps ' + long_name); | ||
Paul Ivanov
|
r14137 | this.test.assertTrue(result2.hasOwnProperty(long_name), | ||
MinRK
|
r14905 | 'fromJSON() embedded ' + short_name + ' gets mime key ' + long_name); | ||
Paul Ivanov
|
r14137 | } | ||
Paul Ivanov
|
r14139 | |||
// helper function for checkout that the first two cells have a particular | ||||
// output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON | ||||
// for a set of mimetype keys, using their short names ('javascript', 'text', | ||||
// 'png', etc). | ||||
function check_output_area(output_type, keys) { | ||||
Paul Ivanov
|
r14144 | this.wait_for_output(0); | ||
Paul Ivanov
|
r14139 | json = this.evaluate(function() { | ||
var json = IPython.notebook.get_cell(0).output_area.toJSON(); | ||||
MinRK
|
r14905 | // appended cell will initially be empty, let's add some output | ||
IPython.notebook.get_cell(1).output_area.fromJSON(json); | ||||
Paul Ivanov
|
r14139 | return json; | ||
}); | ||||
MinRK
|
r14905 | // The evaluate call above happens asynchronously: wait for cell[1] to have output | ||
Paul Ivanov
|
r14185 | this.wait_for_output(1); | ||
Paul Ivanov
|
r14139 | var result = this.get_output_cell(0); | ||
var result2 = this.get_output_cell(1); | ||||
this.test.assertEquals(result.output_type, output_type, | ||||
'testing ' + output_type + ' for ' + keys.join(' and ')); | ||||
for (var idx in keys) { | ||||
assert_has.apply(this, [keys[idx], json, result, result2]); | ||||
} | ||||
} | ||||
Paul Ivanov
|
r14137 | |||
Paul Ivanov
|
r14141 | |||
// helper function to clear the first two cells, set the text of and execute | ||||
// the first one | ||||
function clear_and_execute(that, code) { | ||||
that.evaluate(function() { | ||||
IPython.notebook.get_cell(0).clear_output(); | ||||
IPython.notebook.get_cell(1).clear_output(); | ||||
}); | ||||
that.set_cell_text(0, code); | ||||
that.execute_cell(0); | ||||
} | ||||
Paul Ivanov
|
r14125 | casper.notebook_test(function () { | ||
this.evaluate(function () { | ||||
var cell = IPython.notebook.get_cell(0); | ||||
// "we have to make messes to find out who we are" | ||||
cell.set_text([ | ||||
"%%javascript", | ||||
Paul Ivanov
|
r14126 | "IPython.notebook.insert_cell_below('code')" | ||
Paul Ivanov
|
r14125 | ].join('\n') | ||
); | ||||
cell.execute(); | ||||
}); | ||||
Paul Ivanov
|
r14129 | |||
Paul Ivanov
|
r14125 | this.wait_for_output(0); | ||
this.then(function ( ) { | ||||
Paul Ivanov
|
r14126 | var result = this.get_output_cell(0); | ||
Paul Ivanov
|
r14125 | var num_cells = this.get_cells_length(); | ||
this.test.assertEquals(num_cells, 2, '%%javascript magic works'); | ||||
Paul Ivanov
|
r14126 | this.test.assertTrue(result.hasOwnProperty('application/javascript'), | ||
MinRK
|
r14905 | 'testing JS embedded with mime key'); | ||
Paul Ivanov
|
r14125 | }); | ||
Paul Ivanov
|
r14126 | //this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); | ||
MinRK
|
r14905 | this.then(function () { | ||
clear_and_execute(this, [ | ||||
"%%javascript", | ||||
"var a=5;" | ||||
].join('\n')); | ||||
}); | ||||
Paul Ivanov
|
r14126 | |||
MinRK
|
r14905 | this.then(function () { | ||
Paul Ivanov
|
r14139 | check_output_area.apply(this, ['display_data', ['javascript']]); | ||
Paul Ivanov
|
r14126 | |||
}); | ||||
Paul Ivanov
|
r14125 | |||
Paul Ivanov
|
r14129 | this.then(function() { | ||
Paul Ivanov
|
r14141 | clear_and_execute(this, '%lsmagic'); | ||
Paul Ivanov
|
r14129 | }); | ||
Paul Ivanov
|
r14137 | |||
Paul Ivanov
|
r14139 | this.then(function () { | ||
check_output_area.apply(this, ['pyout', ['text', 'json']]); | ||||
Paul Ivanov
|
r14129 | }); | ||
Paul Ivanov
|
r14139 | |||
Paul Ivanov
|
r14129 | this.then(function() { | ||
Paul Ivanov
|
r14141 | clear_and_execute(this, | ||
Paul Ivanov
|
r14129 | "x = %lsmagic\nfrom IPython.display import display; display(x)"); | ||
}); | ||||
this.then(function ( ) { | ||||
Paul Ivanov
|
r14139 | check_output_area.apply(this, ['display_data', ['text', 'json']]); | ||
Paul Ivanov
|
r14129 | }); | ||
Paul Ivanov
|
r14142 | |||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.display import Latex; Latex('$X^2$')"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['pyout', ['text', 'latex']]); | ||||
}); | ||||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.display import Latex, display; display(Latex('$X^2$'))"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['display_data', ['text', 'latex']]); | ||||
}); | ||||
Paul Ivanov
|
r14143 | |||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.display import HTML; HTML('<b>it works!</b>')"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['pyout', ['text', 'html']]); | ||||
}); | ||||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['display_data', ['text', 'html']]); | ||||
}); | ||||
Paul Ivanov
|
r14129 | |||
Paul Ivanov
|
r14144 | |||
this.then(function() { | ||||
clear_and_execute(this, | ||||
Paul Ivanov
|
r14145 | "from IPython.display import Image; Image(" + black_dot_png + ")"); | ||
Paul Ivanov
|
r14144 | }); | ||
Paul Ivanov
|
r14145 | this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); | ||
Paul Ivanov
|
r14144 | |||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['pyout', ['text', 'png']]); | ||||
}); | ||||
this.then(function() { | ||||
clear_and_execute(this, | ||||
Paul Ivanov
|
r14145 | "from IPython.display import Image, display; display(Image(" + black_dot_png + "))"); | ||
Paul Ivanov
|
r14144 | }); | ||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['display_data', ['text', 'png']]); | ||||
}); | ||||
Paul Ivanov
|
r14145 | |||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['pyout', ['text', 'jpeg']]); | ||||
}); | ||||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['display_data', ['text', 'jpeg']]); | ||||
}); | ||||
Paul Ivanov
|
r14146 | |||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.core.display import SVG; SVG(" + svg + ")"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['pyout', ['text', 'svg']]); | ||||
}); | ||||
this.then(function() { | ||||
clear_and_execute(this, | ||||
"from IPython.core.display import SVG, display; display(SVG(" + svg + "))"); | ||||
}); | ||||
this.then(function ( ) { | ||||
check_output_area.apply(this, ['display_data', ['text', 'svg']]); | ||||
}); | ||||
this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); | ||||
Paul Ivanov
|
r14147 | |||
this.then(function() { | ||||
clear_and_execute(this, [ | ||||
"from IPython.core.formatters import HTMLFormatter", | ||||
"x = HTMLFormatter()", | ||||
"x.format_type = 'text/superfancymimetype'", | ||||
"get_ipython().display_formatter.formatters['text/superfancymimetype'] = x", | ||||
"from IPython.display import HTML, display", | ||||
'display(HTML("yo"))', | ||||
"HTML('hello')"].join('\n') | ||||
); | ||||
}); | ||||
MinRK
|
r14905 | this.wait_for_output(0, 1); | ||
this.then(function () { | ||||
Paul Ivanov
|
r14147 | var long_name = 'text/superfancymimetype'; | ||
var result = this.get_output_cell(0); | ||||
this.test.assertTrue(result.hasOwnProperty(long_name), | ||||
'display_data custom mimetype ' + long_name); | ||||
var result = this.get_output_cell(0, 1); | ||||
this.test.assertTrue(result.hasOwnProperty(long_name), | ||||
'pyout custom mimetype ' + long_name); | ||||
}); | ||||
Paul Ivanov
|
r14125 | }); | ||