-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript-conversion.sh
More file actions
executable file
·51 lines (40 loc) · 1.61 KB
/
Copy pathscript-conversion.sh
File metadata and controls
executable file
·51 lines (40 loc) · 1.61 KB
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
#!/bin/bash
mkdir -p pdfs
# Directorio temporal para las copias sin outputs
tmp_dir=$(mktemp -d)
trap "rm -rf \"$tmp_dir\"" EXIT
# Recopilar y ordenar: primero por directorio, luego por nombre de archivo (ambos alfabéticamente)
mapfile -t notebooks < <(find . -path ./.venv -prune -o -name "*.ipynb" -print | sort)
total=${#notebooks[@]}
padding=${#total}
counter=1
for notebook in "${notebooks[@]}"; do
base_name=$(basename "$notebook" .ipynb)
stripped_name=$(echo "$base_name" | sed 's/^[0-9]*//')
prefix=$(printf "%0${padding}d" "$counter")
output_name="${prefix}_${stripped_name}"
# Subdirectorio propio para evitar colisiones de nombre entre notebooks de distintos directorios
notebook_tmp_dir="${tmp_dir}/${counter}"
mkdir -p "$notebook_tmp_dir"
# Crear copia temporal sin outputs de celdas
jupyter nbconvert --to notebook \
--ClearOutputPreprocessor.enabled=True \
--output-dir "$notebook_tmp_dir" \
"$notebook" 2>/dev/null
tmp_notebook="${notebook_tmp_dir}/${base_name}.ipynb"
if [ -f "$tmp_notebook" ]; then
# Convertir la copia limpia a PDF
jupyter nbconvert --to webpdf "$tmp_notebook" \
--template webpdf_wrap \
--TemplateExporter.extra_template_basedirs="['$(pwd)/nbconvert_templates']" \
--output "$(pwd)/pdfs/${output_name}"
if [ $? -eq 0 ]; then
echo "Converted: $notebook -> pdfs/${output_name}.pdf"
else
echo "Failed to convert: $notebook"
fi
else
echo "Failed to clear outputs for: $notebook"
fi
counter=$((counter + 1))
done