Created
May 6, 2019 08:02
-
-
Save PyroGenesis/008af33225a89e381f1f84a14394051c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This script is used to remove / reset outputs, execution numbers and metadata from .ipynb files for better version control. | |
I created my own script to do this as I could not find any solution that performs all 3 operations properly. | |
Usage: | |
Put this script in the same directory as the jupyter notebook (.ipynb) files and run it. | |
Note: | |
Inspired by the jq method here: http://timstaley.co.uk/posts/making-git-and-jupyter-notebooks-play-nice/ | |
Also this gist: https://gist.github.com/pbugnion/ea2797393033b54674af | |
""" | |
import sys | |
import json | |
import glob | |
def strip_cell(cell): | |
if "outputs" in cell: | |
cell["outputs"] = [] | |
if "execution_count" in cell: | |
cell["execution_count"] = None | |
if "metadata" in cell: | |
cell["metadata"] = {} | |
ipynb_files = glob.glob('*.ipynb') | |
for ipynb_file in ipynb_files: | |
print('Cleaning file: ', ipynb_file) | |
with open(ipynb_file) as f: | |
ipynb = json.load(f) | |
ipy_version = int(ipynb["nbformat"])-1 # nbformat is 1 more than actual version. | |
if ipy_version == 2: | |
for sheet in ipynb["worksheets"]: | |
for cell in sheet["cells"]: | |
strip_cell(cell) | |
else: | |
for cell in ipynb["cells"]: | |
strip_cell(cell) | |
ipynb["metadata"] = {"language_info": {"name":"python", "pygments_lexer": "ipython3"}} | |
json.dump(ipynb, open(ipynb_file, 'w'), sort_keys=True, indent=1, separators=(",",": ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment