13
13
14
14
from jupyter_sphinx .ast import (
15
15
JupyterCellNode ,
16
+ CellInputNode ,
17
+ CellOutputNode ,
16
18
JupyterWidgetViewNode ,
17
19
JupyterWidgetStateNode ,
18
20
cell_output_to_nodes ,
@@ -42,7 +44,7 @@ def doctree(
42
44
apps .append (app )
43
45
app .build ()
44
46
45
- doctree = app .env .get_doctree ("contents" )
47
+ doctree = app .env .get_and_resolve_doctree ("contents" , app . builder )
46
48
47
49
if return_warnings :
48
50
return doctree , warnings .getvalue ()
@@ -66,12 +68,13 @@ def test_basic(doctree):
66
68
"""
67
69
tree = doctree (source )
68
70
(cell ,) = tree .traverse (JupyterCellNode )
71
+ (cellinput , celloutput ) = cell .children
69
72
assert cell .attributes ["code_below" ] is False
70
73
assert cell .attributes ["hide_code" ] is False
71
74
assert cell .attributes ["hide_output" ] is False
72
75
assert cell .attributes ["linenos" ] is False
73
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
74
- assert cell .children [1 ].rawsource .strip () == "4"
76
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2"
77
+ assert celloutput .children [0 ].rawsource .strip () == "4"
75
78
76
79
77
80
def test_basic_old_entrypoint (doctree ):
@@ -82,12 +85,13 @@ def test_basic_old_entrypoint(doctree):
82
85
"""
83
86
tree = doctree (source , entrypoint = "jupyter_sphinx.execute" )
84
87
(cell ,) = tree .traverse (JupyterCellNode )
88
+ (cellinput , celloutput ) = cell .children
85
89
assert cell .attributes ["code_below" ] is False
86
90
assert cell .attributes ["hide_code" ] is False
87
91
assert cell .attributes ["hide_output" ] is False
88
92
assert cell .attributes ["linenos" ] is False
89
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
90
- assert cell .children [1 ].rawsource .strip () == "4"
93
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2"
94
+ assert celloutput .children [0 ].rawsource .strip () == "4"
91
95
92
96
93
97
def test_hide_output (doctree ):
@@ -99,9 +103,10 @@ def test_hide_output(doctree):
99
103
"""
100
104
tree = doctree (source )
101
105
(cell ,) = tree .traverse (JupyterCellNode )
106
+ (cellinput , celloutput ) = cell .children
102
107
assert cell .attributes ["hide_output" ] is True
103
- assert len (cell .children ) == 1
104
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
108
+ assert len (celloutput .children ) == 0
109
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2"
105
110
106
111
107
112
def test_hide_code (doctree ):
@@ -113,9 +118,10 @@ def test_hide_code(doctree):
113
118
"""
114
119
tree = doctree (source )
115
120
(cell ,) = tree .traverse (JupyterCellNode )
121
+ (celloutput ,) = cell .children
116
122
assert cell .attributes ["hide_code" ] is True
117
123
assert len (cell .children ) == 1
118
- assert cell .children [0 ].rawsource .strip () == "4"
124
+ assert celloutput .children [0 ].rawsource .strip () == "4"
119
125
120
126
121
127
def test_code_below (doctree ):
@@ -127,9 +133,10 @@ def test_code_below(doctree):
127
133
"""
128
134
tree = doctree (source )
129
135
(cell ,) = tree .traverse (JupyterCellNode )
136
+ (celloutput , cellinput ) = cell .children
130
137
assert cell .attributes ["code_below" ] is True
131
- assert cell .children [0 ].rawsource .strip () == "4 "
132
- assert cell .children [1 ].rawsource .strip () == "2 + 2 "
138
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2 "
139
+ assert celloutput .children [0 ].rawsource .strip () == "4 "
133
140
134
141
135
142
def test_linenos (doctree ):
@@ -141,10 +148,11 @@ def test_linenos(doctree):
141
148
"""
142
149
tree = doctree (source )
143
150
(cell ,) = tree .traverse (JupyterCellNode )
151
+ (cellinput , celloutput ) = cell .children
144
152
assert cell .attributes ["linenos" ] is True
145
153
assert len (cell .children ) == 2
146
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
147
- assert cell .children [1 ].rawsource .strip () == "4"
154
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2"
155
+ assert celloutput .children [0 ].rawsource .strip () == "4"
148
156
source = """
149
157
.. jupyter-execute::
150
158
:linenos:
@@ -154,6 +162,7 @@ def test_linenos(doctree):
154
162
"""
155
163
tree = doctree (source )
156
164
(cell ,) = tree .traverse (JupyterCellNode )
165
+ (cellinput , celloutput ) = cell .children
157
166
assert len (cell .children ) == 2
158
167
assert cell .attributes ["linenos" ] is True
159
168
@@ -166,10 +175,11 @@ def test_linenos_conf_option(doctree):
166
175
"""
167
176
tree = doctree (source , config = "jupyter_sphinx_linenos = True" )
168
177
(cell ,) = tree .traverse (JupyterCellNode )
169
- assert cell .children [0 ].attributes ["linenos" ]
170
- assert "highlight_args" not in cell .children [0 ].attributes
171
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
172
- assert cell .children [1 ].rawsource .strip () == "4"
178
+ (cellinput , celloutput ) = cell .children
179
+ assert cellinput .attributes ["linenos" ]
180
+ assert "highlight_args" not in cellinput .attributes
181
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2"
182
+ assert celloutput .children [0 ].rawsource .strip () == "4"
173
183
174
184
175
185
def test_continue_linenos_conf_option (doctree ):
@@ -183,9 +193,10 @@ def test_continue_linenos_conf_option(doctree):
183
193
184
194
tree = doctree (source , config = "jupyter_sphinx_continue_linenos = True" )
185
195
(cell ,) = tree .traverse (JupyterCellNode )
186
- assert "linenos" not in cell .children [0 ].attributes
187
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
188
- assert cell .children [1 ].rawsource .strip () == "4"
196
+ (cellinput , celloutput ) = cell .children
197
+ assert "linenos" not in cellinput .attributes
198
+ assert cellinput .children [0 ].rawsource .strip () == "2 + 2"
199
+ assert celloutput .children [0 ].rawsource .strip () == "4"
189
200
190
201
# Test continuous line numbering
191
202
source = """
@@ -206,14 +217,16 @@ def test_continue_linenos_conf_option(doctree):
206
217
)
207
218
208
219
cell0 , cell1 = tree .traverse (JupyterCellNode )
209
- assert cell0 .children [0 ].attributes ["linenos" ]
210
- assert cell0 .children [0 ].rawsource .strip () == "2 + 2"
211
- assert cell0 .children [1 ].rawsource .strip () == "4"
220
+ (cellinput0 , celloutput0 ) = cell0 .children
221
+ (cellinput1 , celloutput1 ) = cell1 .children
222
+ assert cellinput0 .attributes ["linenos" ]
223
+ assert cellinput0 .children [0 ].rawsource .strip () == "2 + 2"
224
+ assert celloutput0 .children [0 ].rawsource .strip () == "4"
212
225
213
- assert cell1 . children [ 0 ] .attributes ["linenos" ]
214
- assert cell1 . children [ 0 ] .attributes ["highlight_args" ]["linenostart" ] == 2
215
- assert cell1 .children [0 ].rawsource .strip () == "3 + 3"
216
- assert cell1 .children [1 ].rawsource .strip () == "6"
226
+ assert cellinput1 .attributes ["linenos" ]
227
+ assert cellinput1 .attributes ["highlight_args" ]["linenostart" ] == 2
228
+ assert cellinput1 .children [0 ].rawsource .strip () == "3 + 3"
229
+ assert celloutput1 .children [0 ].rawsource .strip () == "6"
217
230
218
231
# Line number should continue after lineno-start option
219
232
@@ -234,14 +247,16 @@ def test_continue_linenos_conf_option(doctree):
234
247
"jupyter_sphinx_continue_linenos = True" ,
235
248
)
236
249
cell0 , cell1 = tree .traverse (JupyterCellNode )
237
- assert cell0 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 7
238
- assert cell0 .children [0 ].rawsource .strip () == "2 + 2"
239
- assert cell0 .children [1 ].rawsource .strip () == "4"
250
+ (cellinput0 , celloutput0 ) = cell0 .children
251
+ (cellinput1 , celloutput1 ) = cell1 .children
252
+ assert cellinput0 .attributes ["highlight_args" ]["linenostart" ] == 7
253
+ assert cellinput0 .children [0 ].rawsource .strip () == "2 + 2"
254
+ assert celloutput0 .children [0 ].rawsource .strip () == "4"
240
255
241
- assert cell1 . children [ 0 ] .attributes ["linenos" ]
242
- assert cell1 . children [ 0 ] .attributes ["highlight_args" ]["linenostart" ] == 8
243
- assert cell1 .children [0 ].rawsource .strip () == "3 + 3"
244
- assert cell1 .children [1 ].rawsource .strip () == "6"
256
+ assert cellinput1 .attributes ["linenos" ]
257
+ assert cellinput1 .attributes ["highlight_args" ]["linenostart" ] == 8
258
+ assert cellinput1 .children [0 ].rawsource .strip () == "3 + 3"
259
+ assert celloutput1 .children [0 ].rawsource .strip () == "6"
245
260
246
261
247
262
def test_emphasize_lines (doctree ):
@@ -266,6 +281,8 @@ def test_emphasize_lines(doctree):
266
281
"""
267
282
tree = doctree (source )
268
283
cell0 , cell1 = tree .traverse (JupyterCellNode )
284
+ (cellinput0 , celloutput0 ) = cell0 .children
285
+ (cellinput1 , celloutput1 ) = cell1 .children
269
286
270
287
assert cell0 .attributes ["emphasize_lines" ] == [1 , 3 , 4 , 5 ]
271
288
assert cell1 .attributes ["emphasize_lines" ] == [2 , 4 ]
@@ -284,7 +301,9 @@ def test_execution_environment_carries_over(doctree):
284
301
"""
285
302
tree = doctree (source )
286
303
cell0 , cell1 = tree .traverse (JupyterCellNode )
287
- assert cell1 .children [1 ].rawsource .strip () == "2"
304
+ (cellinput0 , celloutput0 ) = cell0 .children
305
+ (cellinput1 , celloutput1 ) = cell1 .children
306
+ assert celloutput1 .children [0 ].rawsource .strip () == "2"
288
307
289
308
290
309
def test_kernel_restart (doctree ):
@@ -304,7 +323,9 @@ def test_kernel_restart(doctree):
304
323
"""
305
324
tree = doctree (source )
306
325
cell0 , cell1 = tree .traverse (JupyterCellNode )
307
- assert "NameError" in cell1 .children [1 ].rawsource
326
+ (cellinput0 , celloutput0 ) = cell0 .children
327
+ (cellinput1 , celloutput1 ) = cell1 .children
328
+ assert "NameError" in celloutput1 .children [0 ].rawsource
308
329
309
330
310
331
def test_raises (doctree ):
@@ -324,7 +345,8 @@ def test_raises(doctree):
324
345
"""
325
346
tree = doctree (source )
326
347
(cell ,) = tree .traverse (JupyterCellNode )
327
- assert "ValueError" in cell .children [1 ].rawsource
348
+ (cellinput , celloutput ) = cell .children
349
+ assert "ValueError" in celloutput .children [0 ].rawsource
328
350
329
351
source = """
330
352
.. jupyter-execute::
@@ -334,7 +356,8 @@ def test_raises(doctree):
334
356
"""
335
357
tree = doctree (source )
336
358
(cell ,) = tree .traverse (JupyterCellNode )
337
- assert "ValueError" in cell .children [1 ].rawsource
359
+ (cellinput , celloutput ) = cell .children
360
+ assert "ValueError" in celloutput .children [0 ].rawsource
338
361
339
362
340
363
def test_widgets (doctree ):
@@ -370,8 +393,9 @@ def test_stdout(doctree):
370
393
"""
371
394
tree = doctree (source )
372
395
(cell ,) = tree .traverse (JupyterCellNode )
396
+ (cellinput , celloutput ) = cell .children
373
397
assert len (cell .children ) == 2
374
- assert cell .children [1 ].rawsource .strip () == "hello world"
398
+ assert celloutput .children [0 ].rawsource .strip () == "hello world"
375
399
376
400
377
401
def test_stderr (doctree ):
@@ -385,7 +409,8 @@ def test_stderr(doctree):
385
409
tree , warnings = doctree (source , return_warnings = True )
386
410
assert "hello world" in warnings
387
411
(cell ,) = tree .traverse (JupyterCellNode )
388
- assert len (cell .children ) == 1 # no output
412
+ (_ , celloutput ) = cell .children
413
+ assert len (celloutput ) == 0 # no output
389
414
390
415
source = """
391
416
.. jupyter-execute::
@@ -396,9 +421,10 @@ def test_stderr(doctree):
396
421
"""
397
422
tree = doctree (source )
398
423
(cell ,) = tree .traverse (JupyterCellNode )
424
+ (_ , celloutput ) = cell .children
399
425
assert len (cell .children ) == 2
400
- assert "stderr" in cell .children [1 ].attributes ["classes" ]
401
- assert cell .children [1 ].astext ().strip () == "hello world"
426
+ assert "stderr" in celloutput .children [0 ].attributes ["classes" ]
427
+ assert celloutput .children [0 ].astext ().strip () == "hello world"
402
428
403
429
404
430
thebe_config = 'jupyter_sphinx_thebelab_config = {"dummy": True}'
@@ -413,10 +439,11 @@ def test_thebe_hide_output(doctree):
413
439
"""
414
440
tree = doctree (source , thebe_config )
415
441
(cell ,) = tree .traverse (JupyterCellNode )
442
+ (cellinput , celloutput ) = cell .children
416
443
assert cell .attributes ["hide_output" ] is True
417
- assert len (cell .children ) == 1
444
+ assert len (celloutput .children ) == 0
418
445
419
- source = cell .children [0 ]
446
+ source = cellinput .children [0 ]
420
447
assert type (source ) == ThebeSourceNode
421
448
assert len (source .children ) == 1
422
449
assert source .children [0 ].rawsource .strip () == "2 + 2"
@@ -431,16 +458,17 @@ def test_thebe_hide_code(doctree):
431
458
"""
432
459
tree = doctree (source , thebe_config )
433
460
(cell ,) = tree .traverse (JupyterCellNode )
461
+ (cellinput , celloutput ) = cell .children
434
462
assert cell .attributes ["hide_code" ] is True
435
463
assert len (cell .children ) == 2
436
464
437
- source = cell .children [0 ]
465
+ source = cellinput .children [0 ]
438
466
assert type (source ) == ThebeSourceNode
439
467
assert source .attributes ["hide_code" ] is True
440
468
assert len (source .children ) == 1
441
469
assert source .children [0 ].rawsource .strip () == "2 + 2"
442
470
443
- output = cell .children [1 ]
471
+ output = celloutput .children [0 ]
444
472
assert type (output ) == ThebeOutputNode
445
473
assert len (output .children ) == 1
446
474
assert output .children [0 ].rawsource .strip () == "4"
@@ -455,14 +483,15 @@ def test_thebe_code_below(doctree):
455
483
"""
456
484
tree = doctree (source , thebe_config )
457
485
(cell ,) = tree .traverse (JupyterCellNode )
486
+ (cellinput , celloutput ) = cell .children
458
487
assert cell .attributes ["code_below" ] is True
459
488
460
- output = cell .children [0 ]
489
+ output = cellinput .children [0 ]
461
490
assert type (output ) is ThebeOutputNode
462
491
assert len (output .children ) == 1
463
492
assert output .children [0 ].rawsource .strip () == "4"
464
493
465
- source = cell .children [1 ]
494
+ source = celloutput .children [0 ]
466
495
assert type (source ) is ThebeSourceNode
467
496
assert len (source .children ) == 1
468
497
assert source .children [0 ].rawsource .strip () == "2 + 2"
@@ -513,7 +542,8 @@ def test_latex(doctree):
513
542
for start , end in delimiter_pairs :
514
543
tree = doctree (source .format (start , end ))
515
544
(cell ,) = tree .traverse (JupyterCellNode )
516
- assert cell .children [1 ].astext () == r"\int"
545
+ (cellinput , celloutput ) = cell .children
546
+ assert celloutput .children [0 ].astext () == r"\int"
517
547
518
548
519
549
def test_image_mimetype_uri (doctree ):
@@ -523,14 +553,14 @@ def test_image_mimetype_uri(doctree):
523
553
img_locs = ['/_build/jupyter_execute/docs/image_1.png' ,'/_build/jupyter_execute/image_2.png' ]
524
554
525
555
cells = [
526
- {'outputs' :
556
+ {'outputs' :
527
557
[{'data' : {'image/png' : 'Vxb6L1wAAAABJRU5ErkJggg==\n ' , 'text/plain' : '<Figure size 432x288 with 1 Axes>' }, 'metadata' : {'filenames' : {'image/png' : img_locs [0 ]}}, 'output_type' : 'display_data' }]
528
558
},
529
- {'outputs' :
559
+ {'outputs' :
530
560
[{'data' : {'image/png' : 'iVBOJggg==\n ' , 'text/plain' : '<Figure size 432x288 with 1 Axes>' }, 'metadata' : {'filenames' : {'image/png' : img_locs [1 ]}}, 'output_type' : 'display_data' }]
531
561
}]
532
562
533
563
for index , cell in enumerate (cells ):
534
564
cell = from_dict (cell )
535
- output_node = cell_output_to_nodes (cell , priority , True , output_dir , None )
565
+ output_node = cell_output_to_nodes (cell [ "outputs" ] , priority , True , output_dir , None )
536
566
assert output_node [0 ].attributes ['uri' ] == img_locs [index ]
0 commit comments