Skip to content

Commit 4c2ce96

Browse files
committed
Fix running notebook
Various fixes, including jupyter/notebook#7567
1 parent 3b53505 commit 4c2ce96

File tree

6 files changed

+72
-22
lines changed

6 files changed

+72
-22
lines changed

.gitignore

+4
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

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

+6
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

+1
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

+57-22
Original file line numberDiff line numberDiff line change
@@ -1423,10 +1423,12 @@ async def htmx_root_list(
14231423

14241424

14251425
def _get_rootdir(user, root):
1426-
if user and root == "@personal":
1427-
return settings.personal / str(user.id)
1428-
elif user and root == "@shared":
1429-
return settings.shared
1426+
if root == "@personal":
1427+
if user:
1428+
return settings.personal / str(user.id)
1429+
elif root == "@shared":
1430+
if user:
1431+
return settings.shared
14301432
elif root == "@public":
14311433
return settings.public
14321434
else:
@@ -2156,55 +2158,79 @@ def jupyterlite_contents(
21562158

21572159
content = []
21582160

2159-
def directory(path):
2161+
def directory(abspath, relpath):
2162+
stat = abspath.stat()
21602163
return {
2161-
"name": pathlib.Path(path).name,
2162-
"path": path,
2164+
"created": utils.epoch_to_iso(stat.st_ctime),
2165+
"format": "json",
2166+
"hash": None,
2167+
"hash_algorithm": None,
2168+
"last_modified": utils.epoch_to_iso(stat.st_mtime),
2169+
"mimetype": None,
2170+
"name": pathlib.Path(relpath).name,
2171+
"path": relpath,
21632172
"size": None,
21642173
"type": "directory",
2174+
"writable": True,
21652175
}
21662176

21672177
parts = parts[:-1]
21682178
if len(parts) == 0:
2169-
# TODO pub/sub roots: settings.database.roots.values()
2170-
if user:
2171-
content.append(directory("@personal"))
2172-
content.append(directory("@shared"))
2179+
rootdir = _get_rootdir(user, "@personal")
2180+
if rootdir is not None:
2181+
content.append(directory(rootdir, "@personal"))
2182+
2183+
rootdir = _get_rootdir(user, "@shared")
2184+
if rootdir is not None:
2185+
content.append(directory(rootdir, "@shared"))
2186+
2187+
rootdir = _get_rootdir(user, "@public")
2188+
if rootdir is not None:
2189+
content.append(directory(rootdir, "@public"))
21732190

2174-
content.append(directory("@public"))
2191+
# TODO pub/sub roots: settings.database.roots.values()
2192+
response = directory(rootdir.parent, "")
21752193
else:
21762194
root = parts[0]
21772195
rootdir = _get_rootdir(user, root)
21782196
if rootdir is None:
21792197
raise fastapi.HTTPException(status_code=404) # NotFound
21802198

2199+
response = directory(rootdir, root)
21812200
for abspath, relpath in utils.iterdir(rootdir):
21822201
if abspath.is_file():
21832202
if relpath.suffix == ".b2":
21842203
relpath = relpath.with_suffix("")
21852204

21862205
if relpath.suffix == ".ipynb":
2187-
type = "notebook"
2206+
content_type = "notebook"
2207+
writable = True
21882208
else:
2189-
type = "file" # XXX Is this the correct type?
2209+
content_type = "file"
2210+
writable = False
21902211

21912212
stat = abspath.stat()
21922213
content.append(
21932214
{
2215+
"content": None,
21942216
"created": utils.epoch_to_iso(stat.st_ctime),
2217+
"format": None,
2218+
"hash": None,
2219+
"hash_algorithm": None,
21952220
"last_modified": utils.epoch_to_iso(stat.st_mtime),
2221+
"mimetype": None,
21962222
"name": relpath.name,
2197-
"path": relpath,
2223+
"path": f"{root}/{relpath}",
21982224
"size": stat.st_size, # XXX Return the uncompressed size?
2199-
"type": type,
2225+
"type": content_type,
2226+
"writable": writable,
22002227
}
22012228
)
22022229
else:
2203-
content.append(directory(relpath))
2230+
content.append(directory(relpath)) # FIXME
22042231

2205-
return {
2206-
"content": content,
2207-
}
2232+
response["content"] = content
2233+
return response
22082234

22092235

22102236
@app.get("/static/jupyterlite/files/{path:path}")
@@ -2217,11 +2243,20 @@ def jupyterlite_files(
22172243
async def downloader():
22182244
yield await get_file_content(path, user)
22192245

2220-
mimetype = guess_type(path)
2221-
# mimetype = 'application/json'
2246+
# mimetype = guess_type(path)
2247+
mimetype = "application/json"
22222248
return responses.StreamingResponse(downloader(), media_type=mimetype)
22232249

22242250

2251+
@app.get("/service-worker.js")
2252+
def jupyterlite_worker(
2253+
# Query parameters
2254+
enableCache: bool | None = None,
2255+
):
2256+
abspath = BASE_DIR / "static/jupyterlite/service-worker.js"
2257+
return FileResponse(abspath, filename=abspath.name, media_type="application/javascript")
2258+
2259+
22252260
#
22262261
# Static
22272262
#

pyproject.toml

+2
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)