Last active
January 2, 2023 09:35
-
-
Save twolodzko/476073f78664c7b38202c186eb098af1 to your computer and use it in GitHub Desktop.
Clean metadata from Jupyter notebooks
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
#!/bin/bash | |
function usage { | |
echo "Usage: $(basename "$0") [OPTION]... FILE" 2>&1 | |
echo " -i modify the file in place" | |
echo " -o don't strip the outputs" | |
echo " -h display this help and exit" | |
exit 0 | |
} | |
INPLACE=false | |
TMP="$(mktemp)" | |
# shellcheck disable=SC2064 | |
trap "rm -rf $TMP" EXIT | |
# shellcheck disable=SC2220 | |
while getopts ":hio" flag; do | |
case "$flag" in | |
i) INPLACE=true;; | |
o) DONTSTRIP="yes";; | |
h) usage;; | |
esac | |
done | |
for (( i=0; i<OPTIND-1; i++ )); do | |
shift | |
done | |
FILE="$1" | |
if [[ ! -f "$FILE" ]]; then | |
usage | |
fi | |
jq --indent 1 --arg dontstrip "$DONTSTRIP" \ | |
'. | |
| del(.cells[] | select(.source == [])) | |
| if $dontstrip == "yes" then | |
. | |
else | |
(.cells[] | select(has("outputs")) | .outputs) = [] | |
end | |
| (.cells[] | select(has("execution_count")) | .execution_count) = null | |
| .cells[].metadata = {} | |
| .metadata = {} | |
' \ | |
"$FILE" > "$TMP" | |
if $INPLACE; then | |
mv -f "$TMP" "$FILE" | |
else | |
cat "$TMP" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment