From a50bc6c11e6c3b60e6f322fefd5c60ccd32611aa Mon Sep 17 00:00:00 2001
From: Andreas Herten <a.herten@fz-juelich.de>
Date: Sat, 8 May 2021 14:37:24 +0200
Subject: [PATCH] Move to PyPI notebook-splitter over local Python script

---
 Makefile                |  7 ++---
 notebook-task-filter.py | 57 -----------------------------------------
 2 files changed, 4 insertions(+), 60 deletions(-)
 delete mode 100755 notebook-task-filter.py

diff --git a/Makefile b/Makefile
index 4795374..4067a36 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 6fc555e..0000000
--- 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)
-- 
GitLab