Skip to content

Instantly share code, notes, and snippets.

@domvwt
Created January 3, 2022 22:57
Show Gist options
  • Save domvwt/9b799fdc8dfe25004f52001ee78aa6d6 to your computer and use it in GitHub Desktop.
Save domvwt/9b799fdc8dfe25004f52001ee78aa6d6 to your computer and use it in GitHub Desktop.
Substitute Jupyter markdown cell content for text from file
"""
This script searches a Jupyter notebook for markdown cells that start with
three colons (:::) and replaces their content with text from the file path
that follows.
Example cell markup:
':::target_file.md'
Usage:
python substitute-md.py my-report.ipynb
python substitute-md.py my-report.ipynb --output my-report-final.ipynb
"""
import json
from argparse import ArgumentParser
from pathlib import Path
parser = ArgumentParser()
parser.add_argument("source")
parser.add_argument("--output")
args = parser.parse_args()
source = Path(args.source)
target = args.output or f"{source.stem}-new{source.suffix}"
nb = Path(source)
nb_json = json.loads(nb.read_text())
md_cells = [cell for cell in nb_json["cells"] if cell["cell_type"] == "markdown"]
for cell in md_cells:
cell_source = cell["source"]
if len(cell_source) == 1 and cell_source[0].startswith(":::"):
target_file = cell_source[0].lstrip(":")
cell["source"] = [Path(target_file).read_text()]
print(f"Found target: {target_file}")
compiled_nb = json.dumps(nb_json)
Path(target).write_text(compiled_nb)
print(f"Output saved: {target}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment