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 af1fa129f7
commit ac05aa489c
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"
}