-
Notifications
You must be signed in to change notification settings - Fork 3
/
nb_clear_outputs.sh
executable file
·63 lines (45 loc) · 1.23 KB
/
nb_clear_outputs.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
shopt -s globstar
function clear_output()
{
fullfile="$1"
filename=$(basename -- "$fullfile")
tmpname="/tmp/$filename"
rm -f $tmpname
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --to notebook --log-level=WARN --output=$tmpname $fullfile
if [ $? -ne 0 ]; then
echo "fatal: conversion of $fullfile failed"
exit 64
fi
if cmp --silent "$tmpname" "$fullfile"
then
echo "info: $fullfile is clean already!"
else
cp -f $tmpname $fullfile
echo "info: $fullfile has been cleared"
fi
rm -f $tmpname
}
args=("$@")
n_args=${#args[@]}
if [ $n_args == 1 ] && [ "$args" == "--all" ]; then
for i in **/*.ipynb; do # Whitespace-safe and recursive
clear_output "$i"
done
elif [ $n_args > 0 ]; then
for (( i=0;i<$n_args;i++)); do
filename=${args[${i}]}
if [[ ! $filename =~ ^.*\.ipynb$ ]]; then
echo "error: file must have an .ipynb extension"
exit 64
fi
if [[ ! -f $filename ]]; then
echo "error: file $filename does not exist"
exit 64
fi
clear_output "$filename"
done
else
echo "usage: $0 [ --all | [FILE]... ]"
exit 64
fi