aboutsummaryrefslogtreecommitdiff
path: root/clear-outputs.sh
blob: 35eed927b1661581736eb37933d9d8342ea8358d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/sh

set -e

command -v jq > /dev/null || exec guix shell jq -- "$0" "$@"

if [ $# -lt 1 ]; then
    exec find . -name "*.ipynb" -and '!' -wholename "*/.ipynb_checkpoints/*" \
         -exec "$0" '{}' \;
fi

for NOTEBOOK in "$@"; do
    TMP="$(printf %s "$NOTEBOOK" | sed 's/[.]ipynb$/-TMP.ipynb/')"

    jq --indent 1 --sort-keys '
        . + {
            cells: [
                .cells[] |
                (. +
                 if .cell_type == "code" then
                     {outputs: [], execution_count: null}
                 else
                     {}
                 end)
            ]
        }
    ' < "$NOTEBOOK" > "$TMP"

    mv "$TMP" "$NOTEBOOK"
done