Skip to content

Commit

Permalink
docs: add id-field and import to nbexport
Browse files Browse the repository at this point in the history
- Adds `id` field to exported notebooks. For more information see: https://jupyter.org/enhancement-proposals/62-cell-id/cell-id.html
- Import specific exception directly from `nbclient` as a workaround for `nbconvert` not importing it anymore since jupyter/nbconvert@a7df9af .
  • Loading branch information
JoepVanlier committed Dec 18, 2023
1 parent e06ee1c commit 229b0cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
17 changes: 15 additions & 2 deletions docs/_ext/nbexport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import itertools
import mimetypes
import posixpath
Expand All @@ -7,9 +8,13 @@
import nbformat
from sphinx import roles, addnodes
from docutils import nodes, writers
from nbclient.exceptions import CellExecutionError
from docutils.parsers.rst import directives
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _cell_uuid():
return uuid.uuid4().hex[:8]


def _finalize_markdown_cells(nb):
Expand Down Expand Up @@ -92,7 +97,14 @@ def __init__(self, document, app, docpath):
def write_markdown(self, text):
if self.nb.cells[-1].cell_type != "markdown":
self.nb.cells.append(
nbformat.from_dict({"cell_type": "markdown", "metadata": {}, "source": []})
nbformat.from_dict(
{
"cell_type": "markdown",
"metadata": {},
"source": [],
"id": _cell_uuid(),
}
)
)

self.nb.cells[-1].source.append(text.replace("\n", "\n" + " " * self.indent))
Expand All @@ -117,6 +129,7 @@ def add_codecell(self, code):
"metadata": {},
"source": code.strip(),
"outputs": [],
"id": _cell_uuid(),
}
)
)
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sphinx_lfs_content==1.0.0
matplotlib
nbclient>=0.5.0
nbconvert
nbformat
numpy
Expand Down

0 comments on commit 229b0cb

Please sign in to comment.