--- name: jupyter_write kind: function lang: py domain: notebook version: "1.0.0" purity: impure signature: "def jupyter_append_code(notebook_path: str, source: str, server_url: str = 'http://localhost:8888', token: str = '') -> dict" description: "Operaciones de escritura sobre celdas de un notebook Jupyter via colaboracion en tiempo real (WebSocket). Expone cinco operaciones: append_code, append_markdown, insert, edit, delete. NO ejecuta celdas — solo modifica la estructura del notebook." tags: [jupyter, notebook, websocket, cell, write, append, insert, edit, delete, nbmodel] uses_functions: [] uses_types: [] returns: [] returns_optional: false error_type: "error_go_core" imports: [jupyter_nbmodel_client] tested: false tests: [] test_file_path: "" file_path: "python/functions/notebook/jupyter_write.py" --- ## Funciones expuestas | Funcion | Descripcion | |---------|-------------| | `jupyter_append_code(notebook_path, source, server_url, token)` | Anade celda de codigo al final | | `jupyter_append_markdown(notebook_path, source, server_url, token)` | Anade celda markdown al final | | `jupyter_insert_cell(notebook_path, cell_index, source, cell_type, server_url, token)` | Inserta celda en posicion especifica | | `jupyter_edit_cell(notebook_path, cell_index, source, server_url, token)` | Sobrescribe contenido de celda existente | | `jupyter_delete_cell(notebook_path, cell_index, server_url, token)` | Elimina una celda | ## Ejemplo ```python from notebook.jupyter_write import ( jupyter_append_code, jupyter_append_markdown, jupyter_insert_cell, jupyter_edit_cell, jupyter_delete_cell, ) # Anadir celda de codigo al final result = jupyter_append_code( notebook_path="notebooks/01_analisis.ipynb", source="import pandas as pd\ndf = pd.read_csv('data.csv')", server_url="http://localhost:8888", token="mi-token", ) # {"action": "append_code", "cell_index": 5, "notebook": "notebooks/01_analisis.ipynb"} # Anadir celda markdown result = jupyter_append_markdown( notebook_path="notebooks/01_analisis.ipynb", source="## Resultados\n\nAnalisis de los datos obtenidos.", ) # {"action": "append_markdown", "cell_index": 6, "notebook": "notebooks/01_analisis.ipynb"} # Insertar celda en posicion 2 result = jupyter_insert_cell( notebook_path="notebooks/01_analisis.ipynb", cell_index=2, source="# celda insertada", cell_type="code", ) # {"action": "insert", "cell_index": 2, "cell_type": "code", "notebook": "..."} # Editar celda existente (indice 0) result = jupyter_edit_cell( notebook_path="notebooks/01_analisis.ipynb", cell_index=0, source="# Titulo actualizado", ) # {"action": "edit", "cell_index": 0, "notebook": "..."} # Eliminar celda result = jupyter_delete_cell( notebook_path="notebooks/01_analisis.ipynb", cell_index=3, ) # {"action": "delete", "cell_index": 3, "notebook": "..."} ``` ## CLI ```bash # Anadir celda de codigo python -m notebook.jupyter_write append-code notebooks/01.ipynb "print('hola')" --server http://localhost:8888 --token mi-token # Anadir celda markdown python -m notebook.jupyter_write append-markdown notebooks/01.ipynb "## Titulo" # Insertar en posicion 2 python -m notebook.jupyter_write insert notebooks/01.ipynb 2 "x = 42" --type code # Editar celda 0 python -m notebook.jupyter_write edit notebooks/01.ipynb 0 "# Nuevo titulo" # Eliminar celda 3 python -m notebook.jupyter_write delete notebooks/01.ipynb 3 ``` ## Notas - Todas las funciones son sincronas publicamente. Internamente usan `asyncio.run()` sobre corutinas async que se comunican via WebSocket con `NbModelClient`. - El `notebook_path` es relativo al servidor Jupyter (no al filesystem local). - Si el servidor no esta corriendo o el token es incorrecto, lanza excepcion de conexion de `jupyter_nbmodel_client`. - NO ejecuta celdas — solo modifica la estructura. Para ejecutar, usar el MCP de Jupyter o la API REST de Jupyter. - `server_url` y `token` tienen defaults convenientes para desarrollo local (`http://localhost:8888`, token vacio). - El campo `cell_index` en el resultado refleja la posicion final de la celda en el notebook.