Skip to content

Commit d14bdcf

Browse files
committed
Partially support code below option for thebelab
1 parent 7ac4403 commit d14bdcf

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

jupyter_sphinx/execute.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,15 @@ def html(self):
267267
class ThebeSourceNode(docutils.nodes.container):
268268
"""Container that holds the cell source when thebelab is enabled"""
269269

270-
def __init__(self, rawsource='', *children, hide_code, **attributes):
271-
super().__init__('', hide_code=hide_code)
270+
def __init__(self, rawsource='', *children, hide_code, code_below, **attributes):
271+
super().__init__('', hide_code=hide_code, code_below=code_below)
272272

273273
def html(self):
274274
code_class = 'thebelab-code'
275275
if self['hide_code']:
276276
code_class += ' thebelab-hidden'
277+
if self['code_below']:
278+
code_class += ' thebelab-below'
277279
code = self.astext()
278280
return '<pre class="{}" data-executable="true" data-language="python">{}</pre>'\
279281
.format(code_class, code)
@@ -540,16 +542,19 @@ def attach_outputs(output_nodes, node, thebe_config):
540542
if thebe_config and not node.attributes['no_thebelab']:
541543
source = node.children[0]
542544

543-
thebe_source = ThebeSourceNode(hide_code=node.attributes['hide_code'])
545+
thebe_source = ThebeSourceNode(hide_code=node.attributes['hide_code'],
546+
code_below=node.attributes['code_below'])
544547
thebe_source.children = [source]
545548

546549
node.children = [thebe_source]
547550

548551
if not node.attributes['hide_output']:
549-
# We ignore the code_below attribute since this is not supported with thebelab
550552
thebe_output = ThebeOutputNode()
551553
thebe_output.children = output_nodes
552-
node.children.append(thebe_output)
554+
if node.attributes['code_below']:
555+
node.children = [thebe_output] + node.children
556+
else:
557+
node.children = node.children + [thebe_output]
553558
else:
554559
if node.attributes['hide_code']:
555560
node.children = []

jupyter_sphinx/thebelab/thebelab-helper.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ function initThebelab() {
33
if (activateButton.classList.contains('thebelab-active')) {
44
return;
55
}
6+
7+
// Place all outputs below the source where this was not the case
8+
// to make them recognizable by thebelab
9+
let codeBelows = document.getElementsByClassName('thebelab-below');
10+
for(var i = 0; i < codeBelows.length; i++) {
11+
let prev = codeBelows[i]
12+
// Find previous sibling element, compatible with IE8
13+
do prev = prev.previousSibling; while(prev && prev.nodeType !== 1);
14+
swapSibling(prev, codeBelows[i])
15+
}
16+
617
thebelab.bootstrap();
718
activateButton.classList.add('thebelab-active')
819
}
20+
21+
function swapSibling(node1, node2) {
22+
node1.parentNode.replaceChild(node1, node2);
23+
node1.parentNode.insertBefore(node2, node1);
24+
}

0 commit comments

Comments
 (0)