From c2491021ccd627778db9312676e20189eebb6dcf Mon Sep 17 00:00:00 2001
From: Bas Nijholt <basnijholt@gmail.com>
Date: Wed, 16 Oct 2019 14:48:10 +0200
Subject: [PATCH] add ipynb_filter

---
 .gitattributes  |  1 +
 ipynb_filter.py | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 ipynb_filter.py

diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..07d41dd
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+*.ipynb filter=ipynb_filter
diff --git a/ipynb_filter.py b/ipynb_filter.py
new file mode 100644
index 0000000..7165a27
--- /dev/null
+++ b/ipynb_filter.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+# `ipynb_filter.py`:
+# This is a git filters that strips out the outputs and
+# meta data of a Jupyer notebook using `nbconvert`.
+# Execute the following line in order to activate this filter:
+# python ipynb_filter.py
+#
+# The following line should be in `.gitattributes`:
+# *.ipynb filter=ipynb_filter
+#
+# from github.com/basnijholt/ipynb_git_filters
+
+from nbconvert.preprocessors import Preprocessor
+
+
+class RemoveMetadata(Preprocessor):
+    def preprocess(self, nb, resources):
+        nb.metadata = {"language_info": {"name":"python",
+                                         "pygments_lexer": "ipython3"}}
+        return nb, resources
+
+
+if __name__ == '__main__':
+    # The filter is getting activated
+    import os
+    git_cmd = 'git config filter.ipynb_filter.clean "jupyter nbconvert --to notebook --config ipynb_filter.py --stdin --stdout"'
+    os.system(git_cmd)
+else:
+    # This script is used as config
+    c.Exporter.preprocessors = [RemoveMetadata]
+    c.ClearOutputPreprocessor.enabled = True
+    c.ClearOutputPreprocessor.remove_metadata_fields = [
+        "deletable", "editable", "collapsed", "scrolled"]
-- 
GitLab