-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ingest_from_query to run_ingest_query, add process_notebook …
…function, and add tests for notebook processing
- Loading branch information
1 parent
193c3c9
commit 1908391
Showing
6 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" Utilities for processing Jupyter notebooks. """ | ||
|
||
import json | ||
|
||
|
||
def process_notebook(file: str) -> str: | ||
""" | ||
Process a Jupyter notebook file and return an executable Python script as a string. | ||
Parameters | ||
---------- | ||
file : str | ||
The path to the Jupyter notebook file. | ||
Returns | ||
------- | ||
str | ||
The executable Python script as a string. | ||
Raises | ||
------ | ||
ValueError | ||
If an unexpected cell type is encountered. | ||
""" | ||
with open(file, encoding="utf-8") as f: | ||
notebook = json.load(f) | ||
|
||
result = [] | ||
|
||
for cell in notebook["cells"]: | ||
cell_type = cell.get("cell_type") | ||
|
||
# Validate cell type and handle unexpected types | ||
if cell_type not in ("markdown", "code", "raw"): | ||
raise ValueError(f"Unknown cell type: {cell_type}") | ||
|
||
str_ = "".join(cell.get("source", [])) | ||
if not str_: | ||
continue | ||
|
||
# Convert Markdown and raw cells to multi-line comments | ||
if cell_type in ("markdown", "raw"): | ||
str_ = f'"""\n{str_}\n"""' | ||
|
||
result.append(str_) | ||
|
||
return "\n\n".join(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters