Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable pre-notebook cell #211

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ jupyter_sphinx_continue_linenos
Whether to continue line numbering from previous cell in all ``jupyter-execute``
sources.

jupyter_execute_pre_notebook

`\\n` delimited block of code that is inserted to the front of any executed notebooks. Defaults to `None`

Changelog
---------

Expand Down
7 changes: 7 additions & 0 deletions jupyter_sphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ def setup(app):
app.add_config_value("jupyter_sphinx_linenos", False, "env")
app.add_config_value("jupyter_sphinx_continue_linenos", False, "env")

# pre-notebook setup cell
app.add_config_value(
"jupyter_execute_pre_notebook",
None,
"env",
)

# JupyterKernelNode is just a doctree marker for the
# ExecuteJupyterCells transform, so we don't actually render it.
app.add_node(
Expand Down
15 changes: 11 additions & 4 deletions jupyter_sphinx/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,21 @@ def apply(self):
# Add empty placeholder cells for non-executed nodes so nodes
# and cells can be zipped and the provided input/output
# can be inserted later
code_cells = [
nbformat.v4.new_code_cell(node.astext() if node["execute"] else "")
for node in nodes
]
if self.config.jupyter_execute_pre_notebook:
code_cells = [
nbformat.v4.new_code_cell(self.config.jupyter_execute_pre_notebook)
] + code_cells
notebook = execute_cells(
kernel_name,
[
nbformat.v4.new_code_cell(node.astext() if node["execute"] else "")
for node in nodes
],
code_cells,
self.config.jupyter_execute_kwargs,
)
if self.config.jupyter_execute_pre_notebook:
notebook.cells = notebook.cells[1:]

# Raise error if cells raised exceptions and were not marked as doing so
for node, cell in zip(nodes, notebook.cells):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,24 @@ def test_builder_priority(doctree):
_, app, _ = doctree(source, config=config, return_all=True, buildername="latex")
latex = (Path(app.outdir) / "python.tex").read_text()
assert "I am latex" in latex


def test_pre_notebook_cell(doctree):
source = """
.. jupyter-execute::

bool(sys.executable)
"""
tree = doctree(source, config="jupyter_execute_pre_notebook = 'import sys'")
(cell,) = tree.traverse(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert not cell.attributes["code_below"]
assert not cell.attributes["hide_code"]
assert not cell.attributes["hide_output"]
assert not cellinput.children[0]["linenos"]
assert cellinput.children[0].astext().strip() == "bool(sys.executable)"
assert celloutput.children[0].astext().strip() == "True"


if __name__ == "__main__":
pytest.main(["tests/test_execute.py::test_pre_notebook_cell", "-s"])