MCPcopy
hub / github.com/coderamp-labs/gitingest / process_notebook

Function process_notebook

src/gitingest/utils/notebook.py:19–74  ·  view source on GitHub ↗

Process a Jupyter notebook file and return an executable Python script as a string. Parameters ---------- file : Path The path to the Jupyter notebook file. include_output : bool Whether to include cell outputs in the generated script (default: ``True``). Return

(file: Path, *, include_output: bool = True)

Source from the content-addressed store, hash-verified

17
18
19def process_notebook(file: Path, *, include_output: bool = True) -> str:
20 """Process a Jupyter notebook file and return an executable Python script as a string.
21
22 Parameters
23 ----------
24 file : Path
25 The path to the Jupyter notebook file.
26 include_output : bool
27 Whether to include cell outputs in the generated script (default: ``True``).
28
29 Returns
30 -------
31 str
32 The executable Python script as a string.
33
34 Raises
35 ------
36 InvalidNotebookError
37 If the notebook file is invalid or cannot be processed.
38
39 """
40 try:
41 with file.open(encoding="utf-8") as f:
42 notebook: dict[str, Any] = json.load(f)
43 except json.JSONDecodeError as exc:
44 msg = f"Invalid JSON in notebook: {file}"
45 raise InvalidNotebookError(msg) from exc
46
47 # Check if the notebook contains worksheets
48 worksheets = notebook.get("worksheets")
49 if worksheets:
50 logger.warning(
51 "Worksheets are deprecated as of IPEP-17. Consider updating the notebook. "
52 "(See: https://github.com/jupyter/nbformat and "
53 "https://github.com/ipython/ipython/wiki/IPEP-17:-Notebook-Format-4#remove-multiple-worksheets "
54 "for more information.)",
55 )
56
57 if len(worksheets) > 1:
58 logger.warning(
59 "Multiple worksheets detected. Combining all worksheets into a single script.",
60 )
61
62 cells = list(chain.from_iterable(ws["cells"] for ws in worksheets))
63
64 else:
65 cells = notebook["cells"]
66
67 result = ["# Jupyter notebook converted to Python script."]
68
69 for cell in cells:
70 cell_str = _process_cell(cell, include_output=include_output)
71 if cell_str:
72 result.append(cell_str)
73
74 return "\n\n".join(result) + "\n"
75
76

Calls 2

_process_cellFunction · 0.85