Skip to content

Commit 1ae4a45

Browse files
add support for notbook worksheets
1 parent 7659962 commit 1ae4a45

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/gitingest/notebook_utils.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
""" Utilities for processing Jupyter notebooks. """
22

33
import json
4+
import warnings
45
from pathlib import Path
6+
from typing import Any
57

68

79
def process_notebook(file: Path) -> str:
@@ -24,7 +26,23 @@ def process_notebook(file: Path) -> str:
2426
If an unexpected cell type is encountered.
2527
"""
2628
with file.open(encoding="utf-8") as f:
27-
notebook = json.load(f)
29+
notebook: dict[str, Any] = json.load(f)
30+
31+
# Check if the notebook contains worksheets
32+
if worksheets := notebook.get("worksheets"):
33+
# https://github.com/ipython/ipython/wiki/IPEP-17:-Notebook-Format-4#remove-multiple-worksheets
34+
# "The `worksheets` field is a list, but we have no UI to support multiple worksheets.
35+
# Our design has since shifted to heading-cell based structure, so we never intend to
36+
# support the multiple worksheet model. The worksheets list of lists shall be replaced
37+
# with a single list, called `cells`."
38+
warnings.warn("Worksheets are deprecated as of IPEP-17.", DeprecationWarning)
39+
40+
if len(worksheets) > 1:
41+
warnings.warn(
42+
"Multiple worksheets are not supported. Only the first worksheet will be processed.", UserWarning
43+
)
44+
45+
notebook = worksheets[0]
2846

2947
result = []
3048

0 commit comments

Comments
 (0)