15
15
JupyterKernelNode ,
16
16
JupyterWidgetViewNode ,
17
17
JupyterWidgetStateNode ,
18
+ ThebeSourceNode ,
19
+ ThebeOutputNode ,
20
+ ThebeButtonNode ,
18
21
)
19
22
20
23
@pytest .fixture ()
@@ -23,11 +26,13 @@ def doctree():
23
26
apps = []
24
27
syspath = sys .path [:]
25
28
26
- def doctree (source ):
29
+ def doctree (source , config = None ):
27
30
src_dir = tempfile .mkdtemp ()
28
31
source_trees .append (src_dir )
29
32
with open (os .path .join (src_dir , 'conf.py' ), 'w' ) as f :
30
33
f .write ("extensions = ['jupyter_sphinx.execute']" )
34
+ if config is not None :
35
+ f .write ('\n ' + config )
31
36
with open (os .path .join (src_dir , 'index.rst' ), 'w' ) as f :
32
37
f .write (source )
33
38
app = SphinxTestApp (srcdir = path (src_dir ), status = StringIO (),
@@ -56,6 +61,7 @@ def test_basic(doctree):
56
61
assert cell .attributes ['code_below' ] is False
57
62
assert cell .attributes ['hide_code' ] is False
58
63
assert cell .attributes ['hide_output' ] is False
64
+ assert cell .attributes ['no_thebelab' ] is False
59
65
assert cell .children [0 ].rawsource .strip () == "2 + 2"
60
66
assert cell .children [1 ].rawsource .strip () == "4"
61
67
@@ -226,3 +232,107 @@ def test_stderr(doctree):
226
232
cell , = tree .traverse (JupyterCellNode )
227
233
assert len (cell .children ) == 2
228
234
assert cell .children [1 ].rawsource .strip () == "hello world"
235
+
236
+
237
+ thebe_config = "jupyter_sphinx_thebelab_config = {\" dummy\" : True}"
238
+
239
+
240
+ def test_thebe_hide_output (doctree ):
241
+ source = '''
242
+ .. jupyter-execute::
243
+ :hide-output:
244
+
245
+ 2 + 2
246
+ '''
247
+ tree = doctree (source , thebe_config )
248
+ cell , = tree .traverse (JupyterCellNode )
249
+ assert cell .attributes ['hide_output' ] is True
250
+ assert len (cell .children ) == 1
251
+
252
+ source = cell .children [0 ]
253
+ assert type (source ) == ThebeSourceNode
254
+ assert len (source .children ) == 1
255
+ assert source .children [0 ].rawsource .strip () == "2 + 2"
256
+
257
+
258
+ def test_thebe_hide_code (doctree ):
259
+ source = '''
260
+ .. jupyter-execute::
261
+ :hide-code:
262
+
263
+ 2 + 2
264
+ '''
265
+ tree = doctree (source , thebe_config )
266
+ cell , = tree .traverse (JupyterCellNode )
267
+ assert cell .attributes ['hide_code' ] is True
268
+ assert len (cell .children ) == 2
269
+
270
+ source = cell .children [0 ]
271
+ assert type (source ) == ThebeSourceNode
272
+ assert source .attributes ['hide_code' ] is True
273
+ assert len (source .children ) == 1
274
+ assert source .children [0 ].rawsource .strip () == "2 + 2"
275
+
276
+ output = cell .children [1 ]
277
+ assert type (output ) == ThebeOutputNode
278
+ assert len (output .children ) == 1
279
+ assert output .children [0 ].rawsource .strip () == "4"
280
+
281
+
282
+ def test_thebe_code_below (doctree ):
283
+ source = '''
284
+ .. jupyter-execute::
285
+ :code-below:
286
+
287
+ 2 + 2
288
+ '''
289
+ tree = doctree (source , thebe_config )
290
+ cell , = tree .traverse (JupyterCellNode )
291
+ assert cell .attributes ['code_below' ] is True
292
+
293
+ output = cell .children [0 ]
294
+ assert type (output ) is ThebeOutputNode
295
+ assert len (output .children ) == 1
296
+ assert output .children [0 ].rawsource .strip () == "4"
297
+
298
+ source = cell .children [1 ]
299
+ assert type (source ) is ThebeSourceNode
300
+ assert len (source .children ) == 1
301
+ assert source .children [0 ].rawsource .strip () == "2 + 2"
302
+ assert source .attributes ['code_below' ] is True
303
+
304
+
305
+ def test_thebe_button_auto (doctree ):
306
+ config = "jupyter_sphinx_thebelab_config = {\" dummy\" : True}"
307
+ source = """
308
+ .. jupyter-execute::
309
+
310
+ 1 + 1
311
+ """
312
+ tree = doctree (source , config = config )
313
+ assert len (tree .traverse (ThebeButtonNode )) == 1
314
+
315
+
316
+ def test_thebe_button_manual (doctree ):
317
+ config = "jupyter_sphinx_thebelab_config = {\" dummy\" : True}"
318
+ source = """
319
+ .. jupyter-execute::
320
+
321
+ 1 + 1
322
+
323
+ .. thebe-button::
324
+ """
325
+ tree = doctree (source , config )
326
+ assert len (tree .traverse (ThebeButtonNode )) == 1
327
+
328
+
329
+ def test_thebe_button_none (doctree ):
330
+ config = "jupyter_sphinx_thebelab_config = {\" dummy\" : True}"
331
+ source = """
332
+ .. jupyter-execute::
333
+ :no-thebelab:
334
+
335
+ 1 + 1
336
+ """
337
+ tree = doctree (source , config )
338
+ assert len (tree .traverse (ThebeButtonNode )) == 0
0 commit comments