Skip to content
Snippets Groups Projects
Commit a50bc6c1 authored by Andreas Herten's avatar Andreas Herten
Browse files

Move to PyPI notebook-splitter over local Python script

parent e06fdc74
No related branches found
No related tags found
No related merge requests found
Pipeline #67585 passed
# Subnotebooks need https://pypi.org/project/notebook-splitter/
SLIDES = Introduction-to-Pandas--slides.html
SUBNOTEBOOKS = Introduction-to-Pandas--slides.ipynb Introduction-to-Pandas--tasks.ipynb Introduction-to-Pandas--solution.ipynb
......@@ -22,10 +23,10 @@ subnotebooks: $(SUBNOTEBOOKS)
decktape --size "2560x1440" reveal $< $@
Introduction-to-Pandas--slides.ipynb: $(MASTER_NOTEBOOK)
./notebook-task-filter.py $< --keep task --keep solution --keep onlypresentation --remove onlytask --remove onlysolution --remove nopresentation -o $@
notebook-splitter --keep task --keep solution --keep onlypresentation --remove onlytask --remove onlysolution --remove nopresentation -o $@ $<
Introduction-to-Pandas--tasks.ipynb: $(MASTER_NOTEBOOK)
./notebook-task-filter.py $< --keep task --keep nopresentation --keep onlytask --remove solution --remove all -o $@
notebook-splitter --keep task --keep nopresentation --keep onlytask --remove solution --remove all -o $@ $<
Introduction-to-Pandas--solution.ipynb: $(MASTER_NOTEBOOK)
./notebook-task-filter.py $< --keep task --keep nopresentation --keep solution --keep onlysolution --remove all -o $@
notebook-splitter --keep task --keep nopresentation --keep solution --keep onlysolution --remove all -o $@ $<
#!/usr/bin/env python3
# Andreas Herten, 25 February 2019
import json
import copy
import os
import sys
import argparse
def parse(inputfile, keep, remove, basekey):
"""
From inputfile, parse the JSON and remove those cells which have values of the basekey, which are in the list of tags to remove but not in the list of tags to remove.
"""
notebook = json.load(inputfile)
if isinstance(keep, str): keep = [keep]
if isinstance(remove, str): remove = [remove]
notebook_new = copy.deepcopy(notebook)
notebook_new["cells"] = []
for cell in notebook["cells"]:
keepcell = True
for key in remove:
cell_tags = None
if basekey in cell["metadata"]:
cell_tags = cell["metadata"][basekey]
if isinstance(cell_tags, str): cell_tags = [cell_tags]
if cell_tags == None:
if key == "all":
keepcell = False
if cell_tags != None:
if key in cell_tags or key == "all":
for tag in cell_tags:
if tag not in keep:
keepcell = False
if keepcell == True:
notebook_new["cells"].append(cell)
return notebook_new
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Split up Jupyter Notebook by cell metadata.\nSpecify tags to keep with `--keep`, specify tags to remove with `--remove`; multiple instances are ok. Special `--remove` tag: all (which removes all cells except those specified with `--keep`).\nAll tags will be read from BASEKEY, which usually is `exercise`, if you do not specify it differently.')
parser.add_argument('infile', type=argparse.FileType('r'), help="Input file to parse")
parser.add_argument('--output', "-o", type=str, help="Output file to parse")
parser.add_argument('--keep', "-k", action="append", help="Keep these tags.")
parser.add_argument('--remove', "-r", action="append", help="Remove these tags. Special: all (remove all except for keep).")
parser.add_argument('--basekey', type=str, help="Basekey to use for discriminating the tags.", default="exercise")
args = parser.parse_args()
notebook = parse(inputfile=args.infile, keep=args.keep, remove=args.remove, basekey=args.basekey)
if args.output:
with open(args.output, "w") as f:
json.dump(notebook, f)
else:
print(notebook)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment