diff --git a/Makefile b/Makefile index 47953742463d60492ea02f403dfa7c01b01f894c..4067a36b2f8c525c50e18a597ffa296aecd266a2 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +# 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 $@ $< diff --git a/notebook-task-filter.py b/notebook-task-filter.py deleted file mode 100755 index 6fc555e0b8127ed4e145ba06706ee24993745dc4..0000000000000000000000000000000000000000 --- a/notebook-task-filter.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/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)