Skip to content

Commit 76e9fec

Browse files
committed
Added jupyter_sphinx_linenos and jupyter_sphinx_continue_linenos configuration options
1 parent 0962b9b commit 76e9fec

File tree

3 files changed

+117
-124
lines changed

3 files changed

+117
-124
lines changed

doc/source/index.rst

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -193,32 +193,6 @@ produces:
193193
print('B')
194194
print('C')
195195

196-
You may also *continue line numbers* from the previous cell with ``:continue-linenos:``::
197-
198-
.. jupyter-execute::
199-
:linenos:
200-
201-
print('A')
202-
203-
.. jupyter-execute::
204-
:continue-linenos:
205-
206-
print('B')
207-
print('C')
208-
209-
produces:
210-
211-
.. jupyter-execute::
212-
:linenos:
213-
214-
print('A')
215-
216-
.. jupyter-execute::
217-
:continue-linenos:
218-
219-
print('B')
220-
print('C')
221-
222196
Controlling exceptions
223197
----------------------
224198

@@ -353,3 +327,14 @@ jupyter_execute_kwargs
353327

354328
Keyword arguments to pass to ``nbconvert.preprocessors.execute.executenb``, which controls how
355329
code cells are executed. The default is ``dict(timeout=-1, allow_errors=True)``.
330+
331+
jupyter_sphinx_linenos
332+
333+
Boolean argument it use line numbering in code cell display by default. Equivalent
334+
to setting the `linenos` directive for all code cells.
335+
336+
jupyter_sphinx_continue_linenos
337+
338+
Boolean argument to use line numbering in code cells and continue line numbering
339+
from the previous cell. When set, line number can be reset to start at one by using
340+
the directive `linenos`.

jupyter_sphinx/execute.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class JupyterCell(Directive):
131131
If provided, the cell output will not be displayed in the output.
132132
code-below : bool
133133
If provided, the code will be shown below the cell output.
134+
linenos : bool
135+
If provided, the code will be shown with line numbering.
134136
raises : comma separated list of exception types
135137
If provided, a comma-separated list of exception type names that
136138
the cell may raise. If one of the listed execption types is raised
@@ -154,7 +156,7 @@ class JupyterCell(Directive):
154156
'hide-output': directives.flag,
155157
'code-below': directives.flag,
156158
'linenos': directives.flag,
157-
'continue-linenos': directives.flag,
159+
# 'continue-linenos': directives.flag, #<--- remove this
158160
'raises': csv_option,
159161
'stderr': directives.flag,
160162
}
@@ -190,7 +192,7 @@ def run(self):
190192
hide_output=('hide-output' in self.options),
191193
code_below=('code-below' in self.options),
192194
linenos=('linenos' in self.options),
193-
continue_linenos=('continue-linenos' in self.options),
195+
# continue_linenos=('continue-linenos' in self.options),
194196
raises=self.options.get('raises'),
195197
stderr=('stderr' in self.options),
196198
)]
@@ -329,7 +331,8 @@ def apply(self):
329331
default_kernel = self.config.jupyter_execute_default_kernel
330332
default_names = default_notebook_names(docname)
331333
thebe_config = self.config.jupyter_sphinx_thebelab_config
332-
334+
linenos_config = self.config.jupyter_sphinx_linenos
335+
continue_linenos = self.config.jupyter_sphinx_continue_linenos
333336
# Check if we have anything to execute.
334337
if not doctree.traverse(JupyterCellNode):
335338
return
@@ -396,23 +399,24 @@ def apply(self):
396399
source = node.children[0]
397400
source.attributes['language'] = lexer
398401

399-
# Add line numbers to code cells if linenos directive is set.
400-
# Continue line numbers from previous cell with line numbers
401-
# if continue-linenos directive is set.
402+
# Add line numbers to code cells if jupyter_sphinx_linenos or
403+
# jupyter_sphinx_continue_linenos are set in the configuration,
404+
# or the linenos directive is set.
405+
# Reset linenumber if linenos directive is set.
406+
# Update current line numbers from cell if jupyter_sphinx_continue_linenos
407+
# is set.
402408
linenostart = 1
403409
for node in nodes:
404410
source = node.children[0]
405-
if node["continue_linenos"]:
411+
if linenos_config or continue_linenos:
406412
source["linenos"] = True
407-
source["highlight_args"] = {'linenostart': linenostart}
408-
elif node["linenos"]:
413+
if node["linenos"]:
409414
linenostart = 1
410415
source["linenos"] = True
411-
# Advance linenostart or reset it
412-
if node["continue_linenos"] or node["linenos"]:
416+
if continue_linenos:
417+
source["linenos"] = True
418+
source["highlight_args"] = {'linenostart': linenostart}
413419
linenostart += source.rawsource.count("\n") + 1
414-
else:
415-
linenostart = 1
416420

417421
# Write certain cell outputs (e.g. images) to separate files, and
418422
# modify the metadata of the associated cells in 'notebook' to
@@ -791,6 +795,10 @@ def setup(app):
791795

792796
app.add_config_value('jupyter_sphinx_thebelab_url', THEBELAB_URL_DEFAULT, 'html')
793797

798+
# linenos config
799+
app.add_config_value('jupyter_sphinx_linenos', False, 'env')
800+
app.add_config_value('jupyter_sphinx_continue_linenos', False, 'env')
801+
794802
# Used for nodes that do not need to be rendered
795803
def skip(self, node):
796804
raise docutils.nodes.SkipNode

tests/test_execute.py

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -139,91 +139,91 @@ def test_linenos(doctree):
139139
cell, = tree.traverse(JupyterCellNode)
140140
assert len(cell.children) == 2
141141
assert cell.attributes['linenos'] is True
142-
143-
144-
def test_continue_linenos(doctree):
145-
source = '''
146-
.. jupyter-execute::
147-
:linenos:
148-
149-
2 + 2
150-
151-
.. jupyter-execute::
152-
:continue-linenos:
153-
154-
3 + 3
155-
'''
156-
tree = doctree(source)
157-
(cell0, cell1) = tree.traverse(JupyterCellNode)
158-
# :linenos:
159-
assert cell0.attributes['linenos']
160-
assert cell0.attributes['continue_linenos'] is False
161-
assert cell0.children[0].attributes['linenos']
162-
assert 'highlight_args' not in cell0.children[0].attributes
163-
assert cell0.children[0].rawsource.strip() == "2 + 2"
164-
assert cell0.children[1].rawsource.strip() == "4"
165-
# :continue-linenos:
166-
assert cell1.attributes['linenos'] is False
167-
assert cell1.attributes['continue_linenos']
168-
assert 'highlight_args' not in cell1.attributes
169-
assert cell1.children[0].attributes['linenos']
170-
assert cell1.children[0].attributes['highlight_args']['linenostart'] == 2
171-
assert cell1.children[0].rawsource.strip() == "3 + 3"
172-
assert cell1.children[1].rawsource.strip() == "6"
173-
174-
source2 = '''
175-
.. jupyter-execute::
176-
:linenos:
177-
178-
'cell0' # will be line number 1
179-
180-
.. jupyter-execute::
181-
:linenos:
182-
183-
'cell1' # should restart with line number 1
184-
185-
.. jupyter-execute::
186-
:continue-linenos:
187-
188-
'cell2' # should be line number 2
189-
190-
.. jupyter-execute::
191-
192-
'cell3' # no line number directive
193-
194-
.. jupyter-execute::
195-
:continue-linenos:
196-
197-
'cell4' # should restart at line number 1
198-
199-
.. jupyter-execute::
200-
:continue-linenos:
201-
202-
'cell5' # should continue with line number 2
203-
'''
204-
tree2 = doctree(source2)
205-
cells = tree2.traverse(JupyterCellNode)
206-
assert len(cells) == 6
207-
(cell0, cell1, cell2, cell3, cell4, cell5) = cells
208-
# :linenos:
209-
assert cell0.children[0].attributes["linenos"]
210-
assert "highlight_args" not in cell0.children[0].attributes
211-
# :linenos:
212-
assert cell1.children[0].attributes["linenos"]
213-
assert "highlight_args" not in cell1.children[0].attributes
214-
# :continue-linenos:
215-
assert cell2.children[0].attributes["linenos"]
216-
assert cell2.children[0].attributes["highlight_args"]["linenostart"] == 2
217-
# (No line number directive)
218-
assert "linenos" not in cell3.children[0].attributes
219-
assert "highlight_args" not in cell3.children[0].attributes
220-
# :continue-linenos:
221-
assert cell4.children[0].attributes["linenos"]
222-
assert cell4.children[0].attributes["highlight_args"]["linenostart"] == 1
223-
# :continue-linenos:
224-
assert cell5.children[0].attributes["linenos"]
225-
assert cell5.children[0].attributes["highlight_args"]["linenostart"] == 2
226-
142+
#
143+
#
144+
# def test_continue_linenos(doctree):
145+
# source = '''
146+
# .. jupyter-execute::
147+
# :linenos:
148+
#
149+
# 2 + 2
150+
#
151+
# .. jupyter-execute::
152+
# :continue-linenos:
153+
#
154+
# 3 + 3
155+
# '''
156+
# tree = doctree(source)
157+
# (cell0, cell1) = tree.traverse(JupyterCellNode)
158+
# # :linenos:
159+
# assert cell0.attributes['linenos']
160+
# assert cell0.attributes['continue_linenos'] is False
161+
# assert cell0.children[0].attributes['linenos']
162+
# assert 'highlight_args' not in cell0.children[0].attributes
163+
# assert cell0.children[0].rawsource.strip() == "2 + 2"
164+
# assert cell0.children[1].rawsource.strip() == "4"
165+
# # :continue-linenos:
166+
# assert cell1.attributes['linenos'] is False
167+
# assert cell1.attributes['continue_linenos']
168+
# assert 'highlight_args' not in cell1.attributes
169+
# assert cell1.children[0].attributes['linenos']
170+
# assert cell1.children[0].attributes['highlight_args']['linenostart'] == 2
171+
# assert cell1.children[0].rawsource.strip() == "3 + 3"
172+
# assert cell1.children[1].rawsource.strip() == "6"
173+
#
174+
# source2 = '''
175+
# .. jupyter-execute::
176+
# :linenos:
177+
#
178+
# 'cell0' # will be line number 1
179+
#
180+
# .. jupyter-execute::
181+
# :linenos:
182+
#
183+
# 'cell1' # should restart with line number 1
184+
#
185+
# .. jupyter-execute::
186+
# :continue-linenos:
187+
#
188+
# 'cell2' # should be line number 2
189+
#
190+
# .. jupyter-execute::
191+
#
192+
# 'cell3' # no line number directive
193+
#
194+
# .. jupyter-execute::
195+
# :continue-linenos:
196+
#
197+
# 'cell4' # should restart at line number 1
198+
#
199+
# .. jupyter-execute::
200+
# :continue-linenos:
201+
#
202+
# 'cell5' # should continue with line number 2
203+
# '''
204+
# tree2 = doctree(source2)
205+
# cells = tree2.traverse(JupyterCellNode)
206+
# assert len(cells) == 6
207+
# (cell0, cell1, cell2, cell3, cell4, cell5) = cells
208+
# # :linenos:
209+
# assert cell0.children[0].attributes["linenos"]
210+
# assert "highlight_args" not in cell0.children[0].attributes
211+
# # :linenos:
212+
# assert cell1.children[0].attributes["linenos"]
213+
# assert "highlight_args" not in cell1.children[0].attributes
214+
# # :continue-linenos:
215+
# assert cell2.children[0].attributes["linenos"]
216+
# assert cell2.children[0].attributes["highlight_args"]["linenostart"] == 2
217+
# # (No line number directive)
218+
# assert "linenos" not in cell3.children[0].attributes
219+
# assert "highlight_args" not in cell3.children[0].attributes
220+
# # :continue-linenos:
221+
# assert cell4.children[0].attributes["linenos"]
222+
# assert cell4.children[0].attributes["highlight_args"]["linenostart"] == 1
223+
# # :continue-linenos:
224+
# assert cell5.children[0].attributes["linenos"]
225+
# assert cell5.children[0].attributes["highlight_args"]["linenostart"] == 2
226+
#
227227

228228
def test_execution_environment_carries_over(doctree):
229229
source = '''

0 commit comments

Comments
 (0)