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>
This commit is contained in:
2026-04-05 17:11:57 +02:00
parent a9f2c60e3d
commit 9f4ac6de32
6 changed files with 281 additions and 0 deletions
+32
View File
@@ -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.
+32
View File
@@ -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
}
+37
View File
@@ -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.
+59
View File
@@ -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"
}
@@ -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.
+73
View File
@@ -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 <nombre_analysis> [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 <nombre_analysis> [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 "════════════════════════════════════════════════════════════"