Skip to content

Instantly share code, notes, and snippets.

@mpharrigan
Last active January 3, 2019 20:03
Show Gist options
  • Save mpharrigan/14f3c93be5520d139ea265ad95249663 to your computer and use it in GitHub Desktop.
Save mpharrigan/14f3c93be5520d139ea265ad95249663 to your computer and use it in GitHub Desktop.
Clean jupyter notebooks
# http://timstaley.co.uk/posts/making-git-and-jupyter-notebooks-play-nice/
# https://stedolan.github.io/jq/
jq --indent 1 \
'
(.cells[] | select(has("outputs")) | .outputs) = []
| (.cells[] | select(has("execution_count")) | .execution_count) = null
| .metadata = {"language_info": {"name":"python", "pygments_lexer": "ipython3"}}
| .cells[].metadata = {}
' notebook.ipynb
# annoyingly, this prints to stdout and *can't* be redirected to the same file
# as the input without breaking things.
#!/bin/bash
# http://timstaley.co.uk/posts/making-git-and-jupyter-notebooks-play-nice/
# https://stedolan.github.io/jq/
workingfile=`mktemp`
jq --indent 1 \
'
(.cells[] | select(has("outputs")) | .outputs) = []
| (.cells[] | select(has("execution_count")) | .execution_count) = null
| .metadata = {"language_info": {"name":"python", "pygments_lexer": "ipython3"}}
| .cells[].metadata = {}
' $1 > $workingfile
mv $workingfile $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment