Skip to content

Commit e4c07ed

Browse files
committed
Final migration details discovered during deployment
1 parent ed4940e commit e4c07ed

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎Makefile‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ lite-ext:
3535

3636
lite-build:
3737
rm -rf .jupyterlite.doit.db caterva2/services/static/jupyterlite
38-
${BIN}/jupyter lite build --output-dir caterva2/services/static/jupyterlite
38+
# --lite-dir points at the in-package jupyter-lite.json (contentsAllJsonFile),
39+
# so the build picks it up regardless of the working directory (the deploy
40+
# builds from elsewhere). See caterva2/services/lite-config/.
41+
${BIN}/jupyter lite build --lite-dir caterva2/services/lite-config --output-dir caterva2/services/static/jupyterlite
3942

4043
# Installs our jupyterlite fork from a local copy, for development purposes
4144
# Before doing this you must run "make build" in our jupyterlite fork

‎caterva2/services/notebook.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ def inject_pyodide_bootstrap_cell(content: bytes) -> bytes:
4444
except Exception:
4545
return content
4646

47-
for cell in notebook.cells:
48-
if cell.get("metadata", {}).get("caterva2_pyodide_bootstrap"):
49-
return content
47+
# Drop any previously-injected bootstrap cell(s) and prepend a fresh one, so a
48+
# served notebook always carries the *current* bootstrap source. A stale cell may
49+
# have been baked into the saved file (e.g. persisted by save-back, or shipped in
50+
# the notebook); skipping when one exists would keep serving outdated install code.
51+
notebook.cells = [
52+
cell for cell in notebook.cells if not cell.get("metadata", {}).get("caterva2_pyodide_bootstrap")
53+
]
5054

5155
bootstrap_cell = nbformat.v4.new_code_cell(PYODIDE_BOOTSTRAP_CELL_SOURCE)
5256
bootstrap_cell["metadata"]["caterva2_pyodide_bootstrap"] = True

‎caterva2/tests/test_notebook_bootstrap.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import nbformat
22

3-
from caterva2.services.notebook import inject_pyodide_bootstrap_cell
3+
from caterva2.services.notebook import PYODIDE_BOOTSTRAP_CELL_SOURCE, inject_pyodide_bootstrap_cell
44

55

66
def _bootstrap_cells(notebook):
@@ -27,3 +27,19 @@ def test_inject_pyodide_bootstrap_cell_is_idempotent():
2727
patched_notebook = nbformat.reads(patched_twice.decode("utf-8"), as_version=4)
2828

2929
assert len(_bootstrap_cells(patched_notebook)) == 1
30+
31+
32+
def test_inject_pyodide_bootstrap_cell_refreshes_stale_cell():
33+
# A notebook with an OUTDATED bootstrap cell baked in (e.g. persisted by an
34+
# older server / save-back) must be refreshed to the current source, not skipped.
35+
stale = nbformat.v4.new_code_cell("# old install code\nawait micropip.install('blosc2')")
36+
stale["metadata"]["caterva2_pyodide_bootstrap"] = True
37+
notebook = nbformat.v4.new_notebook(cells=[stale, nbformat.v4.new_code_cell("print('hello')")])
38+
content = nbformat.writes(notebook).encode("utf-8")
39+
40+
patched_notebook = nbformat.reads(inject_pyodide_bootstrap_cell(content).decode("utf-8"), as_version=4)
41+
42+
bootstrap = _bootstrap_cells(patched_notebook)
43+
assert len(bootstrap) == 1
44+
assert patched_notebook.cells[0]["metadata"]["caterva2_pyodide_bootstrap"] is True
45+
assert patched_notebook.cells[0].source == PYODIDE_BOOTSTRAP_CELL_SOURCE

‎jupyter-lite.json‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)