Files
fn_registry/python/functions/notebook/jupyter_exec.md
T
egutierrez 611fc81b6b feat: cierra issues 0050 y 0052 + commands automáticos
- 0050: jupyter_exec reescrito sin Y.js (REST + KernelClient). Bug raíz adicional: HEAD /api/contents da 405 → cambiado a GET. 9 tests (5 unit + 4 e2e).
- 0052: footprint_aurgi cerrado. Bug fix en setup_geo_stack_docker_pipeline (verify aborta si compose up falla; nombre de contenedor incorrecto).
- Nueva primitiva docker_container_running_py_infra (7 tests).
- /full-git-push y /full-git-pull pasan a modo automático: auto-commit + push sin preguntar, aborta solo si detecta secrets.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 23:34:03 +02:00

3.8 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
jupyter_exec function py notebook 2.0.0 impure jupyter_append_execute(notebook_path: str, code: str, server_url: str, token: str) -> dict Ejecuta codigo en kernels de Jupyter via REST + WebSocket clasico al kernel. Tres modos: append (añade celda y ejecuta), cell (ejecuta celda existente), kernel (ejecuta sin tocar notebook). NO usa el canal colaborativo Y.js.
jupyter
notebook
kernel
websocket
execution
cells
false error_go_core
jupyter_kernel_client
urllib
json
uuid
name desc
notebook_path Ruta relativa al notebook
name desc
code Código a ejecutar
name desc
server_url URL del servidor Jupyter (default localhost:8888)
name desc
token Token de autenticación (default vacío)
Dict con cell_index y outputs del código ejecutado, o resultados del kernel true
test_notebook_exists_uses_get_not_head
test_notebook_exists_returns_false_on_404
test_create_notebook_skips_when_exists
test_new_code_cell_has_required_fields
test_extract_outputs_handles_streams_and_results
e2e: test_e2e_append_executes_and_persists
e2e: test_e2e_append_twice_increments_index
e2e: test_e2e_cell_executes_existing
e2e: test_e2e_kernel_mode
python/functions/notebook/tests/test_jupyter_exec.py python/functions/notebook/jupyter_exec.py

Funciones

jupyter_append_execute(notebook_path, code, server_url, token)

Añade una celda de codigo al final del notebook, la ejecuta en el kernel y persiste celda + outputs a disco via REST /api/contents. Jupyter Lab detecta el cambio y lo refleja en el browser.

from notebook.jupyter_exec import jupyter_append_execute

result = jupyter_append_execute(
    "notebooks/analisis.ipynb",
    "import pandas as pd\nprint(pd.__version__)",
    server_url="http://localhost:8888",
    token="",
)
# {"cell_index": 5, "outputs": ["2.2.1"]}

jupyter_execute_cell(notebook_path, cell_index, server_url, token)

Ejecuta una celda existente por indice (0-based) y persiste sus outputs.

result = jupyter_execute_cell("notebooks/analisis.ipynb", 3)
# {"cell_index": 3, "outputs": ["42"]}

jupyter_kernel_execute(code, server_url, token)

Ejecuta codigo directo en el kernel sin tocar ningun notebook.

result = jupyter_kernel_execute("len(df)")
# {"outputs": ["1500"], "status": "ok"}

CLI

python -m notebook.jupyter_exec append notebooks/mi.ipynb "print('hola')"
python -m notebook.jupyter_exec cell notebooks/mi.ipynb 2
python -m notebook.jupyter_exec kernel "x = 42; print(x)"

Output siempre JSON. En error retorna {"error": "..."} por stderr con exit code 1.

Extraccion de outputs

output_type campo leido
stream text
display_data / execute_result data.text/plain
error traceback (joined con \n)

Notas (v2.0.0 — fix Issue 0050)

  • Bypassa el canal colaborativo Y.js. Usa REST /api/contents para leer/escribir celdas y KernelClient (websocket clasico al kernel) para ejecutar. Robusto frente a versiones nuevas de jupyter-collaboration que rompian NbModelClient.
  • Trade-off: las celdas/outputs se persisten a disco, no se sincronizan en tiempo real via Y.js. Jupyter Lab detecta el cambio en el filesystem y lo refleja (puede pedir 'Revert to disk' segun version).
  • _notebook_exists usa GET /api/contents?content=0 (HEAD devuelve 405 en Jupyter Server).
  • Auto-init: jupyter_append_execute crea el notebook si no existe y arranca una sesion con kernel si no hay ninguna activa para ese notebook.
  • El token puede ser cadena vacia si el servidor tiene autenticacion deshabilitada.