Skip to content

Commit 9094b7e

Browse files
committed
Fix running notebook
Various fixes, including jupyter/notebook#7567
1 parent f70e4f5 commit 9094b7e

File tree

6 files changed

+72
-22
lines changed

6 files changed

+72
-22
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99

1010
.*.swp
1111
__pycache__
12+
13+
# jupyterlite
14+
/.jupyterlite.doit.db
15+
/caterva2/services/static/jupyterlite/

6941.cc0d6a3.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ install:
1010
${BIN}/pip install -e .[services,hdf5,plugins,blosc2-plugins]
1111
${BIN}/pip install -e .[clients]
1212
${BIN}/pip install -e .[tests]
13+
${BIN}/pip install pre-commit
1314

1415
assets:
1516
rm caterva2/services/static/build/*
@@ -30,3 +31,8 @@ pub-gris:
3031

3132
sub:
3233
BLOSC_TRACE=1 ${BIN}/python3 -m caterva2.services.sub
34+
35+
lite:
36+
rm .jupyterlite.doit.db caterva2/services/static/jupyterlite -rf
37+
${BIN}/jupyter lite build --output-dir caterva2/services/static/jupyterlite
38+
cp 6941.cc0d6a3.js ./caterva2/services/static/jupyterlite/build/

README-JUPYTERLITE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Install:
66
Generate static files:
77

88
cd caterva2/services/static
9+
rm .jupyterlite.doit.db jupyterlite -rf
910
jupyter lite build --output-dir jupyterlite
1011

1112
Usage:

caterva2/services/sub.py

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,10 +1426,12 @@ async def htmx_root_list(
14261426

14271427

14281428
def _get_rootdir(user, root):
1429-
if user and root == "@personal":
1430-
return settings.personal / str(user.id)
1431-
elif user and root == "@shared":
1432-
return settings.shared
1429+
if root == "@personal":
1430+
if user:
1431+
return settings.personal / str(user.id)
1432+
elif root == "@shared":
1433+
if user:
1434+
return settings.shared
14331435
elif root == "@public":
14341436
return settings.public
14351437
else:
@@ -2159,55 +2161,79 @@ def jupyterlite_contents(
21592161

21602162
content = []
21612163

2162-
def directory(path):
2164+
def directory(abspath, relpath):
2165+
stat = abspath.stat()
21632166
return {
2164-
"name": pathlib.Path(path).name,
2165-
"path": path,
2167+
"created": utils.epoch_to_iso(stat.st_ctime),
2168+
"format": "json",
2169+
"hash": None,
2170+
"hash_algorithm": None,
2171+
"last_modified": utils.epoch_to_iso(stat.st_mtime),
2172+
"mimetype": None,
2173+
"name": pathlib.Path(relpath).name,
2174+
"path": relpath,
21662175
"size": None,
21672176
"type": "directory",
2177+
"writable": True,
21682178
}
21692179

21702180
parts = parts[:-1]
21712181
if len(parts) == 0:
2172-
# TODO pub/sub roots: settings.database.roots.values()
2173-
if user:
2174-
content.append(directory("@personal"))
2175-
content.append(directory("@shared"))
2182+
rootdir = _get_rootdir(user, "@personal")
2183+
if rootdir is not None:
2184+
content.append(directory(rootdir, "@personal"))
2185+
2186+
rootdir = _get_rootdir(user, "@shared")
2187+
if rootdir is not None:
2188+
content.append(directory(rootdir, "@shared"))
2189+
2190+
rootdir = _get_rootdir(user, "@public")
2191+
if rootdir is not None:
2192+
content.append(directory(rootdir, "@public"))
21762193

2177-
content.append(directory("@public"))
2194+
# TODO pub/sub roots: settings.database.roots.values()
2195+
response = directory(rootdir.parent, "")
21782196
else:
21792197
root = parts[0]
21802198
rootdir = _get_rootdir(user, root)
21812199
if rootdir is None:
21822200
raise fastapi.HTTPException(status_code=404) # NotFound
21832201

2202+
response = directory(rootdir, root)
21842203
for abspath, relpath in utils.iterdir(rootdir):
21852204
if abspath.is_file():
21862205
if relpath.suffix == ".b2":
21872206
relpath = relpath.with_suffix("")
21882207

21892208
if relpath.suffix == ".ipynb":
2190-
type = "notebook"
2209+
content_type = "notebook"
2210+
writable = True
21912211
else:
2192-
type = "file" # XXX Is this the correct type?
2212+
content_type = "file"
2213+
writable = False
21932214

21942215
stat = abspath.stat()
21952216
content.append(
21962217
{
2218+
"content": None,
21972219
"created": utils.epoch_to_iso(stat.st_ctime),
2220+
"format": None,
2221+
"hash": None,
2222+
"hash_algorithm": None,
21982223
"last_modified": utils.epoch_to_iso(stat.st_mtime),
2224+
"mimetype": None,
21992225
"name": relpath.name,
2200-
"path": relpath,
2226+
"path": f"{root}/{relpath}",
22012227
"size": stat.st_size, # XXX Return the uncompressed size?
2202-
"type": type,
2228+
"type": content_type,
2229+
"writable": writable,
22032230
}
22042231
)
22052232
else:
2206-
content.append(directory(relpath))
2233+
content.append(directory(relpath)) # FIXME
22072234

2208-
return {
2209-
"content": content,
2210-
}
2235+
response["content"] = content
2236+
return response
22112237

22122238

22132239
@app.get("/static/jupyterlite/files/{path:path}")
@@ -2220,11 +2246,20 @@ def jupyterlite_files(
22202246
async def downloader():
22212247
yield await get_file_content(path, user)
22222248

2223-
mimetype = guess_type(path)
2224-
# mimetype = 'application/json'
2249+
# mimetype = guess_type(path)
2250+
mimetype = "application/json"
22252251
return responses.StreamingResponse(downloader(), media_type=mimetype)
22262252

22272253

2254+
@app.get("/service-worker.js")
2255+
def jupyterlite_worker(
2256+
# Query parameters
2257+
enableCache: bool | None = None,
2258+
):
2259+
abspath = BASE_DIR / "static/jupyterlite/service-worker.js"
2260+
return FileResponse(abspath, filename=abspath.name, media_type="application/javascript")
2261+
2262+
22282263
#
22292264
# Static
22302265
#

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ subscriber = [
6565
"pillow",
6666
"python-dotenv",
6767
"python-multipart",
68+
"jupyterlite-core==0.6.0a0",
69+
"jupyterlite-pyodide-kernel==0.6.0a0",
6870
]
6971
services = [
7072
"caterva2[base-services]",

0 commit comments

Comments
 (0)