72c0379c63
Auto-create notebooks y sesiones en jupyter_exec (append y cell). Auto-create en jupyter_write (append_code, append_markdown, batch). Nuevos subcomandos cleanup y shutdown-all en jupyter_kernel. README.md renombrado a README.txt para evitar error de parseo del indexer. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
4.3 KiB
4.3 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 | 1.0.0 | impure | jupyter_append_execute(notebook_path: str, code: str, server_url: str, token: str) -> dict | Ejecuta codigo en kernels de Jupyter via WebSocket. Tres modos: append (añade celda al notebook y la ejecuta), cell (ejecuta celda existente por indice), kernel (ejecuta en el kernel sin tocar ningun notebook). |
|
false | error_go_core |
|
|
Dict con cell_index y outputs del código ejecutado, o resultados del kernel | false | 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 y la ejecuta. Usa el protocolo colaborativo de Jupyter, por lo que tanto el agente como el usuario ven la celda y su output en tiempo real en JupyterLab.
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 del notebook por su indice (0-based).
from notebook.jupyter_exec import jupyter_execute_cell
result = jupyter_execute_cell("notebooks/analisis.ipynb", 3)
# {"cell_index": 3, "outputs": ["42"]}
jupyter_kernel_execute(code, server_url, token)
Ejecuta codigo directamente en el kernel sin modificar ningun notebook. Util para consultas rapidas, inspeccion de variables o verificacion de estado del kernel.
from notebook.jupyter_exec import jupyter_kernel_execute
result = jupyter_kernel_execute("len(df)")
# {"outputs": ["1500"], "status": "ok"}
CLI
# Añadir celda y ejecutar
python -m notebook.jupyter_exec append notebooks/mi.ipynb "print('hola')" --server http://localhost:8888 --token mytoken
# Ejecutar celda existente
python -m notebook.jupyter_exec cell notebooks/mi.ipynb 2 --server http://localhost:8888
# Ejecutar en kernel directamente
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
- Las funciones
appendycellson async internamente; las publicas usanasyncio.run(). jupyter_kernel_executees sincrona directamente porqueKernelClient.executees bloqueante.- El token puede ser cadena vacia si el servidor tiene autenticacion deshabilitada.
NbModelClientrequiere que el servidor tenga habilitado el endpoint colaborativo (/api/collaboration/), disponible en JupyterLab >= 4 conjupyter-collaborationinstalado.- Auto-init:
jupyter_append_executecrea el notebook automaticamente si no existe (via REST PUT /api/contents) y arranca una sesion con kernel si no hay ninguna activa para ese notebook (via POST /api/sessions). No es necesario abrir el notebook manualmente en el navegador. - Auto-session:
jupyter_execute_celltambien garantiza que exista una sesion con kernel antes de ejecutar. - Fix Issue 006:
jupyter_execute_cellnormaliza la celda antes de ejecutar. Las celdas creadas manualmente (no via la UI de Jupyter) pueden carecer deoutputsoexecution_counten el modelo CRDT, lo que causabaKeyError: 'outputs'dentro deexecute_cellal hacerdel ycell["outputs"][:]. El fix lee la celda connb[cell_index], detecta los campos faltantes, y reemplaza la celda vianb[cell_index] = _normalize_code_cell(cell)— que usaset_cellinternamente para re-crear el mapa CRDT completo preservando el source original.