Skip to content

Commit 0252014

Browse files
apeng-stripepre-commit-ci[bot]davidbrochart
authored
Propagate tree path and document mode in page config (#531)
* Propagate tree path and document mode * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * format * add :path to workspaces * fix workspace path * fix signatures * format * handle workspace without tree --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: David Brochart <david.brochart@gmail.com>
1 parent 6cff05f commit 0252014

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

jupyverse_api/jupyverse_api/jupyterlab/__init__.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22

3-
from fastapi import APIRouter, Depends, Request
3+
from fastapi import APIRouter, Depends, HTTPException, Request
44
from fastapi.responses import HTMLResponse
55

66
from jupyverse_api import Config, Router
@@ -19,47 +19,79 @@ def __init__(self, app: App, auth: Auth):
1919
async def get_lab(
2020
user: User = Depends(auth.current_user()),
2121
):
22-
return await self.get_lab(user)
22+
return await self.get_lab("lab", user)
2323

24-
@router.get("/lab/tree/{path:path}")
24+
@router.get("/doc")
25+
async def get_doc(
26+
user: User = Depends(auth.current_user()),
27+
):
28+
return await self.get_lab("doc", user)
29+
30+
@router.get("/{mode}/tree/{path:path}")
2531
async def load_workspace(
32+
mode,
2633
path,
2734
):
28-
return await self.load_workspace(path)
35+
if mode not in {"lab", "doc"}:
36+
raise HTTPException(status_code=404, detail="Not found")
37+
return await self.load_workspace(mode, path)
2938

3039
@router.get("/lab/api/workspaces/{name}")
40+
@router.get("/doc/api/workspaces/{name}")
3141
async def get_workspace_data(user: User = Depends(auth.current_user())):
3242
return await self.get_workspace_data(user)
3343

3444
@router.put(
3545
"/lab/api/workspaces/{name}",
3646
status_code=204,
3747
)
48+
@router.put(
49+
"/doc/api/workspaces/{name}",
50+
status_code=204,
51+
)
3852
async def set_workspace(
3953
request: Request,
4054
user: User = Depends(auth.current_user()),
4155
user_update=Depends(auth.update_user),
4256
):
4357
return await self.set_workspace(request, user, user_update)
4458

45-
@router.get("/lab/workspaces/{name}", response_class=HTMLResponse)
59+
@router.get("/{mode}/workspaces/{name}", response_class=HTMLResponse)
4660
async def get_workspace(
61+
mode,
62+
name,
63+
user: User = Depends(auth.current_user()),
64+
):
65+
if mode not in {"lab", "doc"}:
66+
raise HTTPException(status_code=404, detail="Not found")
67+
68+
return await self.get_workspace(mode, name, "", user)
69+
70+
@router.get("/{mode}/workspaces/{name}/tree/{path:path}", response_class=HTMLResponse)
71+
async def get_workspace_with_tree(
72+
mode,
4773
name,
74+
path,
4875
user: User = Depends(auth.current_user()),
4976
):
50-
return await self.get_workspace(name, user)
77+
if mode not in {"lab", "doc"}:
78+
raise HTTPException(status_code=404, detail="Not found")
79+
80+
return await self.get_workspace(mode, name, path, user)
5181

5282
self.include_router(router)
5383

5484
@abstractmethod
5585
async def get_lab(
5686
self,
87+
mode,
5788
user: User,
5889
): ...
5990

6091
@abstractmethod
6192
async def load_workspace(
6293
self,
94+
mode,
6395
path,
6496
): ...
6597

@@ -77,7 +109,9 @@ async def set_workspace(
77109
@abstractmethod
78110
async def get_workspace(
79111
self,
112+
mode,
80113
name,
114+
path,
81115
user: User,
82116
): ...
83117

plugins/jupyterlab/fps_jupyterlab/routes.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(
5353

5454
async def get_lab(
5555
self,
56+
mode,
5657
user: User,
5758
):
5859
return HTMLResponse(
@@ -62,11 +63,13 @@ async def get_lab(
6263
self.jupyterlab_config.server_side_execution,
6364
self.jupyterlab_config.dev_mode,
6465
self.frontend_config.base_url,
66+
mode=mode,
6567
)
6668
)
6769

6870
async def load_workspace(
6971
self,
72+
mode,
7073
path,
7174
):
7275
return HTMLResponse(
@@ -76,6 +79,8 @@ async def load_workspace(
7679
self.jupyterlab_config.server_side_execution,
7780
self.jupyterlab_config.dev_mode,
7881
self.frontend_config.base_url,
82+
tree_path=path,
83+
mode=mode,
7984
)
8085
)
8186

@@ -96,7 +101,9 @@ async def set_workspace(
96101

97102
async def get_workspace(
98103
self,
104+
mode,
99105
name,
106+
path,
100107
user: User,
101108
):
102109
return self.get_index(
@@ -105,9 +112,20 @@ async def get_workspace(
105112
self.jupyterlab_config.server_side_execution,
106113
self.jupyterlab_config.dev_mode,
107114
self.frontend_config.base_url,
115+
tree_path=path,
116+
mode=mode,
108117
)
109118

110-
def get_index(self, workspace, collaborative, server_side_execution, dev_mode, base_url="/"):
119+
def get_index(
120+
self,
121+
workspace,
122+
collaborative,
123+
server_side_execution,
124+
dev_mode,
125+
base_url="/",
126+
tree_path=None,
127+
mode="lab",
128+
):
111129
for path in self.static_lab_dir.glob("main.*.js"):
112130
main_id = path.name.split(".")[1]
113131
break
@@ -147,7 +165,7 @@ def get_index(self, workspace, collaborative, server_side_execution, dev_mode, b
147165
"licensesUrl": "/lab/api/licenses",
148166
"listingsUrl": "/lab/api/listings",
149167
"mathjaxConfig": "TeX-AMS-MML_HTMLorMML-full,Safe",
150-
"mode": "multiple-document",
168+
"mode": "multiple-document" if mode == "lab" else "single-document",
151169
"notebookVersion": "[1, 9, 0]",
152170
"quitButton": True,
153171
"settingsUrl": "/lab/api/settings",
@@ -160,7 +178,7 @@ def get_index(self, workspace, collaborative, server_side_execution, dev_mode, b
160178
"themesUrl": "/lab/api/themes",
161179
"token": "4e2804532de366abc81e32ab0c6bf68a73716fafbdbb2098",
162180
"translationsApiUrl": "/lab/api/translations",
163-
"treePath": "",
181+
"treePath": "" if tree_path is None else tree_path,
164182
"workspace": workspace,
165183
"treeUrl": "/lab/tree",
166184
"workspacesApiUrl": "/lab/api/workspaces",

0 commit comments

Comments
 (0)