From ac05aa489c573a1cef8877d47b544866d00a846f Mon Sep 17 00:00:00 2001 From: Egutierrez Date: Sun, 5 Apr 2026 17:11:57 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20funciones=20Bash=20=E2=80=94=20install?= =?UTF-8?q?=5Fnbconvert,=20notebook=5Fto=5Fpdf,=20export=5Fanalysis=5Fpdfs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- bash/functions/infra/install_nbconvert.md | 32 ++++++++ bash/functions/infra/install_nbconvert.sh | 32 ++++++++ bash/functions/infra/notebook_to_pdf.md | 37 ++++++++++ bash/functions/infra/notebook_to_pdf.sh | 59 +++++++++++++++ .../pipelines/export_analysis_pdfs.md | 48 ++++++++++++ .../pipelines/export_analysis_pdfs.sh | 73 +++++++++++++++++++ 6 files changed, 281 insertions(+) create mode 100644 bash/functions/infra/install_nbconvert.md create mode 100644 bash/functions/infra/install_nbconvert.sh create mode 100644 bash/functions/infra/notebook_to_pdf.md create mode 100644 bash/functions/infra/notebook_to_pdf.sh create mode 100644 bash/functions/pipelines/export_analysis_pdfs.md create mode 100755 bash/functions/pipelines/export_analysis_pdfs.sh diff --git a/bash/functions/infra/install_nbconvert.md b/bash/functions/infra/install_nbconvert.md new file mode 100644 index 00000000..4586bacc --- /dev/null +++ b/bash/functions/infra/install_nbconvert.md @@ -0,0 +1,32 @@ +--- +name: install_nbconvert +kind: function +lang: bash +domain: infra +version: "1.0.0" +purity: impure +signature: "install_nbconvert(project_dir: string) -> void" +description: "Instala nbconvert y playwright con chromium en un proyecto uv existente. Idempotente: uv add no reinstala si los paquetes ya estan presentes." +tags: [jupyter, nbconvert, pdf, export, playwright, python, uv] +uses_functions: [] +uses_types: [] +returns: [] +returns_optional: false +error_type: "error_go_core" +imports: [] +tested: false +tests: [] +test_file_path: "" +file_path: "bash/functions/infra/install_nbconvert.sh" +--- + +## Ejemplo + +```bash +source install_nbconvert.sh +install_nbconvert /home/lucas/analysis/finanzas +``` + +## Notas + +Requiere que el venv ya exista (usa `init_uv_venv` antes). La instalacion de chromium via `uv run playwright install chromium` puede tardar la primera vez. La salida de playwright se suprime si tiene exito — solo se muestra si hay un error. diff --git a/bash/functions/infra/install_nbconvert.sh b/bash/functions/infra/install_nbconvert.sh new file mode 100644 index 00000000..1c29a907 --- /dev/null +++ b/bash/functions/infra/install_nbconvert.sh @@ -0,0 +1,32 @@ +# install_nbconvert +# ------------------ +# Instala nbconvert y playwright con chromium en un proyecto uv existente. +# Idempotente: uv add no reinstala si los paquetes ya estan presentes. +# +# USO (sourced): +# source install_nbconvert.sh +# install_nbconvert /path/to/project + +install_nbconvert() { + local project_dir="$1" + + if [ -z "$project_dir" ]; then + echo "install_nbconvert: se requiere project_dir" >&2 + return 1 + fi + + if [ ! -d "$project_dir/.venv" ]; then + echo "install_nbconvert: no existe .venv en $project_dir — ejecuta init_uv_venv primero" >&2 + return 1 + fi + + # Instalar nbconvert y playwright via uv add + (cd "$project_dir" && uv add nbconvert playwright 2>&1) + + # Instalar chromium — capturar output, solo mostrar si hay error + local playwright_output + if ! playwright_output=$(cd "$project_dir" && uv run playwright install chromium 2>&1); then + echo "$playwright_output" >&2 + return 1 + fi +} diff --git a/bash/functions/infra/notebook_to_pdf.md b/bash/functions/infra/notebook_to_pdf.md new file mode 100644 index 00000000..088fb76f --- /dev/null +++ b/bash/functions/infra/notebook_to_pdf.md @@ -0,0 +1,37 @@ +--- +name: notebook_to_pdf +kind: function +lang: bash +domain: infra +version: "1.0.0" +purity: impure +signature: "notebook_to_pdf(project_dir: string, [pattern: string], [output_dir: string]) -> string" +description: "Convierte notebooks Jupyter a PDF usando nbconvert webpdf con chromium. Lista los PDFs generados al finalizar." +tags: [jupyter, notebook, pdf, export, nbconvert, playwright] +uses_functions: [] +uses_types: [] +returns: [] +returns_optional: false +error_type: "error_go_core" +imports: [] +tested: false +tests: [] +test_file_path: "" +file_path: "bash/functions/infra/notebook_to_pdf.sh" +--- + +## Ejemplo + +```bash +source notebook_to_pdf.sh + +# Con defaults (notebooks/*.ipynb -> notebooks/pdf/) +notebook_to_pdf /home/lucas/analysis/finanzas + +# Con pattern y output_dir custom +notebook_to_pdf /home/lucas/analysis/finanzas "notebooks/01_*.ipynb" "exports/pdf/" +``` + +## Notas + +Requiere nbconvert y playwright con chromium instalados (usa `install_nbconvert` antes). Usa el venv del proyecto directamente (`.venv/bin/jupyter`). El output_dir es relativo a project_dir. Imprime los PDFs generados con sus rutas al finalizar. Falla si no se genera ningun PDF. diff --git a/bash/functions/infra/notebook_to_pdf.sh b/bash/functions/infra/notebook_to_pdf.sh new file mode 100644 index 00000000..1ea9b86a --- /dev/null +++ b/bash/functions/infra/notebook_to_pdf.sh @@ -0,0 +1,59 @@ +# 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" +} diff --git a/bash/functions/pipelines/export_analysis_pdfs.md b/bash/functions/pipelines/export_analysis_pdfs.md new file mode 100644 index 00000000..22f2fc04 --- /dev/null +++ b/bash/functions/pipelines/export_analysis_pdfs.md @@ -0,0 +1,48 @@ +--- +name: export_analysis_pdfs +kind: pipeline +lang: bash +domain: pipelines +version: "1.0.0" +purity: impure +signature: "export_analysis_pdfs(nombre: string, [pattern: string]) -> void" +description: "Exporta todos los notebooks de un analisis Jupyter a PDF. Instala nbconvert y playwright automaticamente si no estan presentes." +tags: [pipeline, jupyter, pdf, export, nbconvert, launcher] +uses_functions: + - assert_command_exists_bash_shell + - install_nbconvert_bash_infra + - notebook_to_pdf_bash_infra +uses_types: [] +returns: [] +returns_optional: false +error_type: "error_go_core" +imports: [] +tested: false +tests: [] +test_file_path: "" +file_path: "bash/functions/pipelines/export_analysis_pdfs.sh" +--- + +## Ejemplo + +```bash +# Exportar todos los notebooks de un analisis +./export_analysis_pdfs.sh finanzas + +# Con pattern especifico +./export_analysis_pdfs.sh ml "notebooks/01_*.ipynb" + +# Via fn run +fn run export_analysis_pdfs finanzas +fn run export_analysis_pdfs ml "notebooks/01_*.ipynb" +``` + +## Flujo + +1. `assert_command_exists uv` — verifica que uv esta disponible +2. `install_nbconvert` — instala nbconvert y playwright con chromium (idempotente) +3. `notebook_to_pdf` — convierte notebooks al patron indicado a PDF en `notebooks/pdf/` + +## Notas + +El analysis debe existir previamente en `analysis/{nombre}/` con un venv inicializado. Los PDFs se generan en `analysis/{nombre}/notebooks/pdf/` por defecto. El pipeline usa `set -euo pipefail` — cualquier fallo detiene la ejecucion. diff --git a/bash/functions/pipelines/export_analysis_pdfs.sh b/bash/functions/pipelines/export_analysis_pdfs.sh new file mode 100755 index 00000000..e849ddd3 --- /dev/null +++ b/bash/functions/pipelines/export_analysis_pdfs.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# export_analysis_pdfs +# --------------------- +# Pipeline que exporta todos los notebooks de un analisis a PDF. +# Compone: assert_command_exists + install_nbconvert + notebook_to_pdf +# +# USO: +# ./export_analysis_pdfs.sh [pattern] +# +# EJEMPLOS: +# ./export_analysis_pdfs.sh finanzas +# ./export_analysis_pdfs.sh ml "notebooks/01_*.ipynb" + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REGISTRY_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" + +# Source funciones atomicas +source "$REGISTRY_ROOT/bash/functions/shell/assert_command_exists.sh" +source "$REGISTRY_ROOT/bash/functions/infra/install_nbconvert.sh" +source "$REGISTRY_ROOT/bash/functions/infra/notebook_to_pdf.sh" + +# ── Argumentos ────────────────────────────────────────────── + +NOMBRE="${1:-}" +if [ -z "$NOMBRE" ]; then + echo "Uso: $0 [pattern]" >&2 + echo " Ejemplo: $0 finanzas" >&2 + echo " Ejemplo: $0 ml 'notebooks/01_*.ipynb'" >&2 + exit 1 +fi +shift +PATTERN="${1:-notebooks/*.ipynb}" + +ANALYSIS_DIR="${REGISTRY_ROOT}/analysis/${NOMBRE}" + +# Verificar que el analysis existe +if [ ! -d "$ANALYSIS_DIR" ]; then + echo "Error: analysis '${NOMBRE}' no existe en ${ANALYSIS_DIR}" >&2 + exit 1 +fi + +echo "" +echo "════════════════════════════════════════════════════════════" +echo " EXPORT ANALYSIS PDFs: ${NOMBRE}" +echo " Directorio: ${ANALYSIS_DIR}" +echo "════════════════════════════════════════════════════════════" +echo "" + +# ── 1. Verificar herramientas ─────────────────────────────── + +echo "[1/3] Verificando herramientas..." +assert_command_exists uv +echo " OK" + +# ── 2. Instalar nbconvert + playwright ────────────────────── + +echo "[2/3] Instalando dependencias de exportacion..." +install_nbconvert "$ANALYSIS_DIR" +echo " OK" + +# ── 3. Convertir notebooks a PDF ──────────────────────────── + +echo "[3/3] Convirtiendo notebooks a PDF..." +notebook_to_pdf "$ANALYSIS_DIR" "$PATTERN" + +# ── Resumen ───────────────────────────────────────────────── + +echo "" +echo "════════════════════════════════════════════════════════════" +echo " EXPORT COMPLETADO" +echo "════════════════════════════════════════════════════════════"