Show More
@@ -1,235 +1,237 | |||
|
1 | 1 | // Test opening a rich notebook, saving it, and reopening it again. |
|
2 | 2 | // |
|
3 | 3 | //toJSON fromJSON toJSON and do a string comparison |
|
4 | 4 | |
|
5 | 5 | |
|
6 | 6 | // this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js |
|
7 | 7 | mime = { |
|
8 | 8 | "text" : "text/plain", |
|
9 | 9 | "html" : "text/html", |
|
10 | 10 | "svg" : "image/svg+xml", |
|
11 | 11 | "png" : "image/png", |
|
12 | 12 | "jpeg" : "image/jpeg", |
|
13 | 13 | "latex" : "text/latex", |
|
14 | 14 | "json" : "application/json", |
|
15 | 15 | "javascript" : "application/javascript", |
|
16 | 16 | }; |
|
17 | 17 | |
|
18 | 18 | 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==\"\"\""; |
|
19 | 19 | var black_dot_png = 'u\"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QA\\niAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AA\\nAAACAAHiIbwzAAAAAElFTkSuQmCC\"'; |
|
20 | 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 | 21 | |
|
22 | 22 | // helper function to ensure that the short_name is found in the toJSON |
|
23 | 23 | // represetnation, while the original in-memory cell retains its long mimetype |
|
24 | 24 | // name, and that fromJSON also gets its long mimetype name |
|
25 | 25 | function assert_has(short_name, json, result, result2) { |
|
26 | 26 | long_name = mime[short_name]; |
|
27 | 27 | this.test.assertTrue(json[0].hasOwnProperty(short_name), |
|
28 | 28 | 'toJSON() representation uses ' + short_name); |
|
29 | 29 | this.test.assertTrue(result.hasOwnProperty(long_name), |
|
30 | 30 | 'toJSON() original embeded JSON keeps ' + long_name); |
|
31 | 31 | this.test.assertTrue(result2.hasOwnProperty(long_name), |
|
32 | 32 | 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | // helper function for checkout that the first two cells have a particular |
|
36 | 36 | // output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON |
|
37 | 37 | // for a set of mimetype keys, using their short names ('javascript', 'text', |
|
38 | 38 | // 'png', etc). |
|
39 | 39 | function check_output_area(output_type, keys) { |
|
40 | 40 | this.wait_for_output(0); |
|
41 | 41 | json = this.evaluate(function() { |
|
42 | 42 | var json = IPython.notebook.get_cell(0).output_area.toJSON(); |
|
43 | 43 | // appended cell will initially be empty, lets add it some output |
|
44 | 44 | var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json); |
|
45 | 45 | return json; |
|
46 | 46 | }); |
|
47 | // The evaluate call above happens asyncrhonously: wait for cell[1] to have output | |
|
48 | this.wait_for_output(1); | |
|
47 | 49 | var result = this.get_output_cell(0); |
|
48 | 50 | var result2 = this.get_output_cell(1); |
|
49 | 51 | this.test.assertEquals(result.output_type, output_type, |
|
50 | 52 | 'testing ' + output_type + ' for ' + keys.join(' and ')); |
|
51 | 53 | |
|
52 | 54 | for (var idx in keys) { |
|
53 | 55 | assert_has.apply(this, [keys[idx], json, result, result2]); |
|
54 | 56 | } |
|
55 | 57 | } |
|
56 | 58 | |
|
57 | 59 | |
|
58 | 60 | // helper function to clear the first two cells, set the text of and execute |
|
59 | 61 | // the first one |
|
60 | 62 | function clear_and_execute(that, code) { |
|
61 | 63 | that.evaluate(function() { |
|
62 | 64 | IPython.notebook.get_cell(0).clear_output(); |
|
63 | 65 | IPython.notebook.get_cell(1).clear_output(); |
|
64 | 66 | }); |
|
65 | 67 | that.set_cell_text(0, code); |
|
66 | 68 | that.execute_cell(0); |
|
67 | 69 | } |
|
68 | 70 | |
|
69 | 71 | casper.notebook_test(function () { |
|
70 | 72 | this.evaluate(function () { |
|
71 | 73 | var cell = IPython.notebook.get_cell(0); |
|
72 | 74 | // "we have to make messes to find out who we are" |
|
73 | 75 | cell.set_text([ |
|
74 | 76 | "%%javascript", |
|
75 | 77 | "IPython.notebook.insert_cell_below('code')" |
|
76 | 78 | ].join('\n') |
|
77 | 79 | ); |
|
78 | 80 | |
|
79 | 81 | cell.execute(); |
|
80 | 82 | }); |
|
81 | 83 | |
|
82 | 84 | this.wait_for_output(0); |
|
83 | 85 | |
|
84 | 86 | this.then(function ( ) { |
|
85 | 87 | var result = this.get_output_cell(0); |
|
86 | 88 | var num_cells = this.get_cells_length(); |
|
87 | 89 | this.test.assertEquals(num_cells, 2, '%%javascript magic works'); |
|
88 | 90 | this.test.assertTrue(result.hasOwnProperty('application/javascript'), |
|
89 | 91 | 'testing JS embeded with mime key'); |
|
90 | 92 | }); |
|
91 | 93 | |
|
92 | 94 | //this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); |
|
93 | 95 | |
|
94 | 96 | this.then(function ( ) { |
|
95 | 97 | check_output_area.apply(this, ['display_data', ['javascript']]); |
|
96 | 98 | |
|
97 | 99 | }); |
|
98 | 100 | |
|
99 | 101 | this.then(function() { |
|
100 | 102 | clear_and_execute(this, '%lsmagic'); |
|
101 | 103 | }); |
|
102 | 104 | |
|
103 | 105 | this.then(function () { |
|
104 | 106 | check_output_area.apply(this, ['pyout', ['text', 'json']]); |
|
105 | 107 | }); |
|
106 | 108 | |
|
107 | 109 | this.then(function() { |
|
108 | 110 | clear_and_execute(this, |
|
109 | 111 | "x = %lsmagic\nfrom IPython.display import display; display(x)"); |
|
110 | 112 | }); |
|
111 | 113 | |
|
112 | 114 | this.then(function ( ) { |
|
113 | 115 | check_output_area.apply(this, ['display_data', ['text', 'json']]); |
|
114 | 116 | }); |
|
115 | 117 | |
|
116 | 118 | this.then(function() { |
|
117 | 119 | clear_and_execute(this, |
|
118 | 120 | "from IPython.display import Latex; Latex('$X^2$')"); |
|
119 | 121 | }); |
|
120 | 122 | |
|
121 | 123 | this.then(function ( ) { |
|
122 | 124 | check_output_area.apply(this, ['pyout', ['text', 'latex']]); |
|
123 | 125 | }); |
|
124 | 126 | |
|
125 | 127 | this.then(function() { |
|
126 | 128 | clear_and_execute(this, |
|
127 | 129 | "from IPython.display import Latex, display; display(Latex('$X^2$'))"); |
|
128 | 130 | }); |
|
129 | 131 | |
|
130 | 132 | this.then(function ( ) { |
|
131 | 133 | check_output_area.apply(this, ['display_data', ['text', 'latex']]); |
|
132 | 134 | }); |
|
133 | 135 | |
|
134 | 136 | this.then(function() { |
|
135 | 137 | clear_and_execute(this, |
|
136 | 138 | "from IPython.display import HTML; HTML('<b>it works!</b>')"); |
|
137 | 139 | }); |
|
138 | 140 | |
|
139 | 141 | this.then(function ( ) { |
|
140 | 142 | check_output_area.apply(this, ['pyout', ['text', 'html']]); |
|
141 | 143 | }); |
|
142 | 144 | |
|
143 | 145 | this.then(function() { |
|
144 | 146 | clear_and_execute(this, |
|
145 | 147 | "from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))"); |
|
146 | 148 | }); |
|
147 | 149 | |
|
148 | 150 | this.then(function ( ) { |
|
149 | 151 | check_output_area.apply(this, ['display_data', ['text', 'html']]); |
|
150 | 152 | }); |
|
151 | 153 | |
|
152 | 154 | |
|
153 | 155 | this.then(function() { |
|
154 | 156 | clear_and_execute(this, |
|
155 | 157 | "from IPython.display import Image; Image(" + black_dot_png + ")"); |
|
156 | 158 | }); |
|
157 | 159 | this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); |
|
158 | 160 | |
|
159 | 161 | this.then(function ( ) { |
|
160 | 162 | check_output_area.apply(this, ['pyout', ['text', 'png']]); |
|
161 | 163 | }); |
|
162 | 164 | |
|
163 | 165 | this.then(function() { |
|
164 | 166 | clear_and_execute(this, |
|
165 | 167 | "from IPython.display import Image, display; display(Image(" + black_dot_png + "))"); |
|
166 | 168 | }); |
|
167 | 169 | |
|
168 | 170 | this.then(function ( ) { |
|
169 | 171 | check_output_area.apply(this, ['display_data', ['text', 'png']]); |
|
170 | 172 | }); |
|
171 | 173 | |
|
172 | 174 | |
|
173 | 175 | this.then(function() { |
|
174 | 176 | clear_and_execute(this, |
|
175 | 177 | "from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')"); |
|
176 | 178 | }); |
|
177 | 179 | |
|
178 | 180 | this.then(function ( ) { |
|
179 | 181 | check_output_area.apply(this, ['pyout', ['text', 'jpeg']]); |
|
180 | 182 | }); |
|
181 | 183 | |
|
182 | 184 | this.then(function() { |
|
183 | 185 | clear_and_execute(this, |
|
184 | 186 | "from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))"); |
|
185 | 187 | }); |
|
186 | 188 | |
|
187 | 189 | this.then(function ( ) { |
|
188 | 190 | check_output_area.apply(this, ['display_data', ['text', 'jpeg']]); |
|
189 | 191 | }); |
|
190 | 192 | |
|
191 | 193 | this.then(function() { |
|
192 | 194 | clear_and_execute(this, |
|
193 | 195 | "from IPython.core.display import SVG; SVG(" + svg + ")"); |
|
194 | 196 | }); |
|
195 | 197 | |
|
196 | 198 | this.then(function ( ) { |
|
197 | 199 | check_output_area.apply(this, ['pyout', ['text', 'svg']]); |
|
198 | 200 | }); |
|
199 | 201 | |
|
200 | 202 | this.then(function() { |
|
201 | 203 | clear_and_execute(this, |
|
202 | 204 | "from IPython.core.display import SVG, display; display(SVG(" + svg + "))"); |
|
203 | 205 | }); |
|
204 | 206 | |
|
205 | 207 | this.then(function ( ) { |
|
206 | 208 | check_output_area.apply(this, ['display_data', ['text', 'svg']]); |
|
207 | 209 | }); |
|
208 | 210 | |
|
209 | 211 | this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); |
|
210 | 212 | |
|
211 | 213 | this.then(function() { |
|
212 | 214 | clear_and_execute(this, [ |
|
213 | 215 | "from IPython.core.formatters import HTMLFormatter", |
|
214 | 216 | "x = HTMLFormatter()", |
|
215 | 217 | "x.format_type = 'text/superfancymimetype'", |
|
216 | 218 | "get_ipython().display_formatter.formatters['text/superfancymimetype'] = x", |
|
217 | 219 | "from IPython.display import HTML, display", |
|
218 | 220 | 'display(HTML("yo"))', |
|
219 | 221 | "HTML('hello')"].join('\n') |
|
220 | 222 | ); |
|
221 | 223 | |
|
222 | 224 | }); |
|
223 | 225 | |
|
224 | 226 | this.then(function ( ) { |
|
225 | 227 | var long_name = 'text/superfancymimetype'; |
|
226 | 228 | var result = this.get_output_cell(0); |
|
227 | 229 | this.test.assertTrue(result.hasOwnProperty(long_name), |
|
228 | 230 | 'display_data custom mimetype ' + long_name); |
|
229 | 231 | var result = this.get_output_cell(0, 1); |
|
230 | 232 | this.test.assertTrue(result.hasOwnProperty(long_name), |
|
231 | 233 | 'pyout custom mimetype ' + long_name); |
|
232 | 234 | |
|
233 | 235 | }); |
|
234 | 236 | |
|
235 | 237 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now