Files
fn_registry/bash/functions/infra/notebook_to_pdf.sh
egutierrez 9f4ac6de32 feat: funciones Bash — install_nbconvert, notebook_to_pdf, export_analysis_pdfs
Infra: install_nbconvert (instala nbconvert+deps), notebook_to_pdf (convierte .ipynb a PDF).
Pipeline: export_analysis_pdfs (exporta todos los notebooks de analysis/ a PDF).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 17:11:57 +02:00

60 lines
1.8 KiB
Bash

# notebook_to_pdf
# ----------------
# Convierte notebooks Jupyter a PDF usando nbconvert webpdf.
# Requiere nbconvert y playwright con chromium instalados.
#
# USO (sourced):
# source notebook_to_pdf.sh
# notebook_to_pdf /path/to/project
# notebook_to_pdf /path/to/project "notebooks/*.ipynb" "notebooks/pdf/"
notebook_to_pdf() {
local project_dir="$1"
local pattern="${2:-notebooks/*.ipynb}"
local output_dir="${3:-notebooks/pdf/}"
if [ -z "$project_dir" ]; then
echo "notebook_to_pdf: se requiere project_dir" >&2
return 1
fi
if [ ! -d "$project_dir/.venv" ]; then
echo "notebook_to_pdf: no existe .venv en $project_dir" >&2
return 1
fi
# Crear directorio de salida si no existe
mkdir -p "$project_dir/$output_dir"
# Convertir notebooks a PDF con nbconvert webpdf
# nbconvert puede retornar exit != 0 por warnings de validacion JSON
# que no impiden la generacion del PDF, asi que ignoramos el exit code
# y verificamos que los PDFs se hayan generado
local nbconvert_output
nbconvert_output=$(cd "$project_dir" && \
.venv/bin/jupyter nbconvert \
--to webpdf \
--allow-chromium-download \
--output-dir="$output_dir" \
$pattern 2>&1) || true
echo "$nbconvert_output"
# Listar PDFs generados
echo ""
echo "PDFs generados en ${project_dir}/${output_dir}:"
local pdf_count=0
while IFS= read -r -d '' pdf; do
echo " $pdf"
pdf_count=$((pdf_count + 1))
done < <(find "$project_dir/$output_dir" -name "*.pdf" -print0 2>/dev/null)
if [ "$pdf_count" -eq 0 ]; then
echo " (ninguno encontrado — nbconvert pudo haber fallado)" >&2
echo "$nbconvert_output" >&2
return 1
fi
echo " Total: $pdf_count PDFs"
}