Skip to content

Commit 761a29c

Browse files
committed
Support custom drives
1 parent 154edfe commit 761a29c

12 files changed

Lines changed: 288 additions & 8 deletions

File tree

‎packages/_metapackage/style/index.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
77
@import url('~@jupyter-notebook/application/style/index.css');
8+
@import url('~@jupyter-notebook/tree/style/index.css');
89
@import url('~@jupyter-notebook/ui-components/style/index.css');
910
@import url('~@jupyter-notebook/application-extension/style/index.css');
1011
@import url('~@jupyter-notebook/console-extension/style/index.css');
@@ -14,5 +15,4 @@
1415
@import url('~@jupyter-notebook/lab-extension/style/index.css');
1516
@import url('~@jupyter-notebook/notebook-extension/style/index.css');
1617
@import url('~@jupyter-notebook/terminal-extension/style/index.css');
17-
@import url('~@jupyter-notebook/tree/style/index.css');
1818
@import url('~@jupyter-notebook/tree-extension/style/index.css');

‎packages/_metapackage/style/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
77
import '@jupyter-notebook/application/style/index.js';
8+
import '@jupyter-notebook/tree/style/index.js';
89
import '@jupyter-notebook/ui-components/style/index.js';
910
import '@jupyter-notebook/application-extension/style/index.js';
1011
import '@jupyter-notebook/console-extension/style/index.js';
@@ -14,5 +15,4 @@ import '@jupyter-notebook/help-extension/style/index.js';
1415
import '@jupyter-notebook/lab-extension/style/index.js';
1516
import '@jupyter-notebook/notebook-extension/style/index.js';
1617
import '@jupyter-notebook/terminal-extension/style/index.js';
17-
import '@jupyter-notebook/tree/style/index.js';
1818
import '@jupyter-notebook/tree-extension/style/index.js';

‎packages/application-extension/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
},
4141
"dependencies": {
4242
"@jupyter-notebook/application": "^7.7.0-beta.3",
43+
"@jupyter-notebook/tree": "^7.7.0-beta.3",
4344
"@jupyter-notebook/ui-components": "^7.7.0-beta.3",
4445
"@jupyterlab/application": "~4.7.0-beta.3",
4546
"@jupyterlab/apputils": "~4.8.0-beta.3",
4647
"@jupyterlab/console": "~4.7.0-beta.3",
4748
"@jupyterlab/coreutils": "~6.7.0-beta.3",
4849
"@jupyterlab/docmanager": "~4.7.0-beta.3",
4950
"@jupyterlab/docregistry": "~4.7.0-beta.3",
51+
"@jupyterlab/filebrowser": "~4.7.0-beta.3",
5052
"@jupyterlab/mainmenu": "~4.7.0-beta.3",
5153
"@jupyterlab/rendermime": "~4.7.0-beta.3",
5254
"@jupyterlab/settingregistry": "~4.7.0-beta.3",

‎packages/application-extension/src/index.ts‎

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { IDocumentManager, renameDialog } from '@jupyterlab/docmanager';
3131

3232
import { DocumentRegistry, DocumentWidget } from '@jupyterlab/docregistry';
3333

34+
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
35+
3436
import { IMainMenu } from '@jupyterlab/mainmenu';
3537

3638
import {
@@ -60,6 +62,8 @@ import {
6062
defaultNotebookPathOpener,
6163
} from '@jupyter-notebook/application';
6264

65+
import { INotebookTree } from '@jupyter-notebook/tree';
66+
6367
import { jupyterIcon } from '@jupyter-notebook/ui-components';
6468

6569
import { SettingConnector } from './settingconnector';
@@ -170,6 +174,55 @@ const dirty: JupyterFrontEndPlugin<void> = {
170174
},
171175
};
172176

177+
/**
178+
* A plugin to show the file browsers of the custom drives on the tree page
179+
* only, as tabs of the tree view.
180+
*/
181+
const drives: JupyterFrontEndPlugin<void> = {
182+
id: '@jupyter-notebook/application-extension:drives',
183+
description:
184+
'A plugin to show the file browsers of the custom drives on the tree page only, as tabs of the tree view.',
185+
requires: [INotebookShell, IFileBrowserFactory],
186+
optional: [INotebookTree],
187+
autoStart: true,
188+
activate: (
189+
app: JupyterFrontEnd,
190+
notebookShell: INotebookShell,
191+
factory: IFileBrowserFactory,
192+
notebookTree: INotebookTree | null
193+
) => {
194+
const { tracker } = factory;
195+
196+
/**
197+
* Move a side panel widget out of its panel if it holds the file browser
198+
* of a custom drive. Extensions add this file browser to a side panel,
199+
* sometimes inside another widget.
200+
*/
201+
const move = (widget: Widget) => {
202+
const browser = tracker.find(
203+
(browser) => !!browser.model.driveName && widget.contains(browser)
204+
);
205+
if (!browser) {
206+
return;
207+
}
208+
if (!notebookTree) {
209+
// The other pages show a document, not the files.
210+
widget.parent = null;
211+
return;
212+
}
213+
// Side panel widgets usually have a caption and no label.
214+
widget.title.label ||= widget.title.caption || browser.model.driveName;
215+
notebookTree.addWidget(widget);
216+
};
217+
218+
const { leftHandler, rightHandler } = notebookShell;
219+
for (const handler of [leftHandler, rightHandler]) {
220+
handler.widgets.forEach(move);
221+
handler.widgetAdded.connect((sender, widget) => move(widget));
222+
}
223+
},
224+
};
225+
173226
/**
174227
* The application info.
175228
*/
@@ -1169,15 +1222,17 @@ const sidePanelVisibility: JupyterFrontEndPlugin<void> = {
11691222
});
11701223

11711224
// Update menu and palette when widgets are added or removed from side panels.
1172-
notebookShell.leftHandler.widgetAdded.connect((sidePanel, widget) => {
1173-
sidePanelPalette.addItem(widget, sidePanel.area);
1174-
});
1225+
const addItem = (sidePanel: SidePanelHandler, widget: Widget) => {
1226+
// A plugin can move the widget out of the panel during the signal.
1227+
if (sidePanel.has(widget.id)) {
1228+
sidePanelPalette.addItem(widget, sidePanel.area);
1229+
}
1230+
};
1231+
notebookShell.leftHandler.widgetAdded.connect(addItem);
11751232
notebookShell.leftHandler.widgetRemoved.connect((sidePanel, widget) => {
11761233
sidePanelPalette.removeItem(widget, sidePanel.area);
11771234
});
1178-
notebookShell.rightHandler.widgetAdded.connect((sidePanel, widget) => {
1179-
sidePanelPalette.addItem(widget, sidePanel.area);
1180-
});
1235+
notebookShell.rightHandler.widgetAdded.connect(addItem);
11811236
notebookShell.rightHandler.widgetRemoved.connect(
11821237
(sidePanel, widget) => {
11831238
sidePanelPalette.removeItem(widget, sidePanel.area);
@@ -1478,6 +1533,7 @@ namespace Private {
14781533
*/
14791534
const plugins: JupyterFrontEndPlugin<any>[] = [
14801535
dirty,
1536+
drives,
14811537
info,
14821538
logo,
14831539
menus,

‎packages/application-extension/style/index.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
/* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
77
@import url('~@lumino/widgets/style/index.css');
88
@import url('~@jupyter-notebook/application/style/index.css');
9+
@import url('~@jupyterlab/filebrowser/style/index.css');
10+
@import url('~@jupyter-notebook/tree/style/index.css');
911
@import url('./base.css');

‎packages/application-extension/style/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
/* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
77
import '@lumino/widgets/style/index.js';
88
import '@jupyter-notebook/application/style/index.js';
9+
import '@jupyterlab/filebrowser/style/index.js';
10+
import '@jupyter-notebook/tree/style/index.js';
911

1012
import './base.css';

‎packages/application-extension/tsconfig.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
{
1010
"path": "../application"
1111
},
12+
{
13+
"path": "../tree"
14+
},
1215
{
1316
"path": "../ui-components"
1417
}

‎ui-tests/test/drives.spec.ts‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) Jupyter Development Team.
2+
// Distributed under the terms of the Modified BSD License.
3+
4+
import path from 'path';
5+
6+
import { expect } from '@jupyterlab/galata';
7+
8+
import type { Locator, Page } from '@playwright/test';
9+
10+
import { test } from './fixtures';
11+
12+
const NOTEBOOK = 'simple.ipynb';
13+
14+
const SUBFOLDER = 'subfolder';
15+
16+
test.use({ autoGoto: false });
17+
18+
/**
19+
* Open a folder in the file browser of the drive, from the root of the drive.
20+
*/
21+
async function openFolder(browser: Locator, folder: string): Promise<void> {
22+
for (const name of folder.split('/')) {
23+
await browser
24+
.locator('.jp-DirListing-item')
25+
.getByText(name, { exact: true })
26+
.dblclick();
27+
}
28+
await expect(browser.locator('.jp-BreadCrumbs')).toContainText(
29+
path.basename(folder)
30+
);
31+
}
32+
33+
/**
34+
* Create a notebook from the main menu and return the path of the page that
35+
* opens it.
36+
*/
37+
async function newNotebook(page: Page): Promise<string> {
38+
const [notebook] = await Promise.all([
39+
page.waitForEvent('popup'),
40+
page.menu.clickMenuItem('File>New>Notebook'),
41+
]);
42+
await notebook.waitForSelector('.jp-NotebookPanel');
43+
const pathname = decodeURIComponent(new URL(notebook.url()).pathname);
44+
await notebook.close();
45+
return pathname;
46+
}
47+
48+
test.describe('Custom drive', () => {
49+
let drive: Locator;
50+
let driveTab: Locator;
51+
52+
test.beforeEach(async ({ context, page, tmpPath }) => {
53+
// The `test-drive` extension only adds its drive when the tests opt in.
54+
await context.addInitScript(() => {
55+
window.localStorage.setItem('test-drive', 'true');
56+
});
57+
await page.contents.uploadFile(
58+
path.resolve(__dirname, `./notebooks/${NOTEBOOK}`),
59+
`${tmpPath}/${SUBFOLDER}/${NOTEBOOK}`
60+
);
61+
await page.goto(`tree/${tmpPath}`);
62+
63+
drive = page.locator('#main-panel #test-drive-browser');
64+
driveTab = page.locator('.jp-TreePanel .lm-TabBar-tab', {
65+
hasText: 'Test Drive',
66+
});
67+
});
68+
69+
test('should show the file browser of the drive in a tab', async ({
70+
page,
71+
}) => {
72+
await expect(page.locator('#main-panel #filebrowser')).toBeVisible();
73+
await expect(drive).toBeHidden();
74+
75+
await driveTab.click();
76+
77+
await expect(drive).toBeVisible();
78+
await expect(page.locator('#main-panel #filebrowser')).toBeHidden();
79+
});
80+
81+
test('should create a notebook in the file browser of the current tab', async ({
82+
page,
83+
tmpPath,
84+
}) => {
85+
// The drive is added last, it must not replace the default file browser.
86+
expect(await newNotebook(page)).toEqual(
87+
`/notebooks/${tmpPath}/Untitled.ipynb`
88+
);
89+
90+
await driveTab.click();
91+
await openFolder(drive, `${tmpPath}/${SUBFOLDER}`);
92+
expect(await newNotebook(page)).toEqual(
93+
`/notebooks/TestDrive:${tmpPath}/${SUBFOLDER}/Untitled.ipynb`
94+
);
95+
96+
await page.locator('.jp-TreePanel >> text="Files"').click();
97+
expect(await newNotebook(page)).toEqual(
98+
`/notebooks/${tmpPath}/Untitled1.ipynb`
99+
);
100+
});
101+
102+
test('should open a notebook of the drive', async ({ page, tmpPath }) => {
103+
await driveTab.click();
104+
await openFolder(drive, `${tmpPath}/${SUBFOLDER}`);
105+
106+
const [notebook] = await Promise.all([
107+
page.waitForEvent('popup'),
108+
drive.getByText(NOTEBOOK, { exact: true }).dblclick(),
109+
]);
110+
111+
await expect(notebook.locator('.jp-Notebook')).toContainText(
112+
'Test Notebook'
113+
);
114+
expect(decodeURIComponent(new URL(notebook.url()).pathname)).toEqual(
115+
`/notebooks/TestDrive:${tmpPath}/${SUBFOLDER}/${NOTEBOOK}`
116+
);
117+
});
118+
119+
test('should not show the file browser of the drive on the notebook page', async ({
120+
page,
121+
tmpPath,
122+
}) => {
123+
await page.goto(`notebooks/TestDrive:${tmpPath}/${SUBFOLDER}/${NOTEBOOK}`);
124+
125+
// The page uses the drive, but has no file browser for it.
126+
await expect(page.locator('.jp-Notebook')).toContainText('Test Notebook');
127+
await expect(page.locator('#test-drive-browser')).toHaveCount(0);
128+
});
129+
});

‎ui-tests/test/jupyter_server_config.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import Any
23

34
from jupyterlab.galata import configure_jupyter_server
@@ -6,3 +7,9 @@
67
c.JupyterNotebookApp.expose_app_in_browser = True
78

89
configure_jupyter_server(c)
10+
11+
# Add the extensions for the tests to the test helpers of Galata.
12+
c.LabServerApp.extra_labextensions_path = [
13+
c.LabServerApp.extra_labextensions_path,
14+
str(Path(__file__).parent / "labextensions"),
15+
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "test-drive",
3+
"version": "0.1.0",
4+
"private": true,
5+
"description": "A prebuilt extension that adds a custom drive, for the UI tests",
6+
"jupyterlab": {
7+
"extension": true,
8+
"_build": {
9+
"load": "static/remoteEntry.js",
10+
"extension": "./extension"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)