Skip to content

Commit 5689420

Browse files
authored
Create launcher button (#97)
1 parent e68c896 commit 5689420

File tree

7 files changed

+129
-13
lines changed

7 files changed

+129
-13
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"@jupyterlab/apputils": "^4.0.0",
6363
"@jupyterlab/coreutils": "^6.0.0",
6464
"@jupyterlab/docregistry": "^4.0.0",
65+
"@jupyterlab/launcher": "^4.0.2",
6566
"@jupyterlab/mainmenu": "^4.0.0",
6667
"@jupyterlab/notebook": "^4.0.0",
6768
"@jupyterlab/observables": "^5.0.0",

src/commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* The command IDs used by the control panel.
33
*/
44
export namespace CommandIDs {
5+
export const createNew = 'glue-control:new-session';
6+
57
export const new1DHistogram = 'glue-control:new-1d-histogram-viewer';
68

79
export const new2DImage = 'glue-control:new-2d-image-viewer';

src/document/default.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"DataCollection": {
3+
"_protocol": 4,
4+
"_type": "glue.core.data_collection.DataCollection",
5+
"cids": [],
6+
"components": [],
7+
"data": [],
8+
"groups": [],
9+
"links": [],
10+
"subset_group_count": 0
11+
},
12+
"Session": {
13+
"_type": "glue.core.session.Session"
14+
},
15+
"__main__": {
16+
"_type": "glue.app.qt.application.GlueApplication",
17+
"data": "DataCollection",
18+
"plugins": [],
19+
"session": "Session",
20+
"tab_names": ["Tab 1"],
21+
"viewers": [[]]
22+
}
23+
}

src/document/plugin.ts

+76-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
2-
import {
3-
JupyterFrontEnd,
4-
JupyterFrontEndPlugin
5-
} from '@jupyterlab/application';
6-
import { WidgetTracker } from '@jupyterlab/apputils';
7-
81
import {
92
ICollaborativeDrive,
103
SharedDocumentFactory
114
} from '@jupyter/docprovider';
5+
import {
6+
JupyterFrontEnd,
7+
JupyterFrontEndPlugin
8+
} from '@jupyterlab/application';
9+
import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils';
10+
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
11+
import { ILauncher } from '@jupyterlab/launcher';
12+
import { INotebookTracker } from '@jupyterlab/notebook';
13+
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
14+
import { IJupyterYWidgetManager } from 'yjs-widgets';
1215

16+
import { CommandIDs } from '../commands';
1317
import { IGlueSessionTracker } from '../token';
18+
import { glueIcon } from '../tools';
19+
import defaultContent from './default.json';
1420
import { GlueSessionModelFactory } from './modelFactory';
21+
import { GlueSessionSharedModel } from './sharedModel';
1522
import { GlueSessionTracker } from './tracker';
1623
import { GlueCanvasWidgetFactory } from './widgetFactory';
17-
import { GlueSessionSharedModel } from './sharedModel';
18-
import { INotebookTracker } from '@jupyterlab/notebook';
19-
import { IJupyterYWidgetManager } from 'yjs-widgets';
2024

2125
const NAME_SPACE = 'gluepyter';
2226

@@ -97,3 +101,65 @@ export const gluePlugin: JupyterFrontEndPlugin<void> = {
97101
);
98102
}
99103
};
104+
105+
/**
106+
* Add launcher button to create a new glue session.
107+
*/
108+
export const newFilePlugin: JupyterFrontEndPlugin<void> = {
109+
id: 'gluepyter:create-new-plugin',
110+
autoStart: true,
111+
requires: [IFileBrowserFactory],
112+
optional: [ILauncher, ICommandPalette],
113+
activate: (
114+
app: JupyterFrontEnd,
115+
browserFactory: IFileBrowserFactory,
116+
launcher?: ILauncher,
117+
commandPalette?: ICommandPalette
118+
) => {
119+
const { commands } = app;
120+
commands.addCommand(CommandIDs.createNew, {
121+
label: args => 'New Glue Session',
122+
caption: 'Create a new Glue Session',
123+
icon: args => (args['isPalette'] ? undefined : glueIcon),
124+
execute: async args => {
125+
const cwd = (args['cwd'] ||
126+
browserFactory.tracker.currentWidget?.model.path) as string;
127+
128+
let model = await app.serviceManager.contents.newUntitled({
129+
path: cwd,
130+
type: 'file',
131+
ext: '.glu'
132+
});
133+
model = await app.serviceManager.contents.save(model.path, {
134+
...model,
135+
format: 'text',
136+
size: undefined,
137+
content: JSON.stringify(defaultContent)
138+
});
139+
140+
// Open the newly created file with the 'Editor'
141+
return app.commands.execute('docmanager:open', {
142+
path: model.path
143+
});
144+
}
145+
});
146+
147+
// Add the command to the launcher
148+
if (launcher) {
149+
launcher.add({
150+
command: CommandIDs.createNew,
151+
category: 'Gluepyter',
152+
rank: 1
153+
});
154+
}
155+
156+
// Add the command to the palette
157+
if (commandPalette) {
158+
commandPalette.addItem({
159+
command: CommandIDs.createNew,
160+
args: { isPalette: true },
161+
category: 'Gluepyter'
162+
});
163+
}
164+
}
165+
};

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { controlPanel } from './leftPanel/plugin';
2-
import { gluePlugin, sessionTrackerPlugin } from './document/plugin';
2+
import {
3+
gluePlugin,
4+
sessionTrackerPlugin,
5+
newFilePlugin
6+
} from './document/plugin';
37
import { yGlueSessionWidgetPlugin } from './yWidget';
48

59
export default [
610
sessionTrackerPlugin,
711
gluePlugin,
812
controlPanel,
9-
yGlueSessionWidgetPlugin
13+
yGlueSessionWidgetPlugin,
14+
newFilePlugin
1015
];

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"target": "ES2018",
2121
"types": ["jest"]
2222
},
23-
"include": ["src/**/*"]
23+
"include": ["src/**/*", "src/document/*.json"]
2424
}

yarn.lock

+19
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,24 @@ __metadata:
24312431
languageName: node
24322432
linkType: hard
24332433

2434+
"@jupyterlab/launcher@npm:^4.0.2":
2435+
version: 4.0.2
2436+
resolution: "@jupyterlab/launcher@npm:4.0.2"
2437+
dependencies:
2438+
"@jupyterlab/apputils": ^4.1.2
2439+
"@jupyterlab/translation": ^4.0.2
2440+
"@jupyterlab/ui-components": ^4.0.2
2441+
"@lumino/algorithm": ^2.0.0
2442+
"@lumino/commands": ^2.1.1
2443+
"@lumino/coreutils": ^2.1.1
2444+
"@lumino/disposable": ^2.1.1
2445+
"@lumino/properties": ^2.0.0
2446+
"@lumino/widgets": ^2.1.1
2447+
react: ^18.2.0
2448+
checksum: 4415fb8d2db8701857f21c515a8d4680133ffb5023b005681abd5f2df353a6cbb0e16099e35f363df44f015d4784a267c355c807ab840a15f63f1f3c219c7d11
2449+
languageName: node
2450+
linkType: hard
2451+
24342452
"@jupyterlab/lsp@npm:^4.0.2":
24352453
version: 4.0.2
24362454
resolution: "@jupyterlab/lsp@npm:4.0.2"
@@ -6220,6 +6238,7 @@ __metadata:
62206238
"@jupyterlab/builder": ^4.0.0
62216239
"@jupyterlab/coreutils": ^6.0.0
62226240
"@jupyterlab/docregistry": ^4.0.0
6241+
"@jupyterlab/launcher": ^4.0.2
62236242
"@jupyterlab/mainmenu": ^4.0.0
62246243
"@jupyterlab/notebook": ^4.0.0
62256244
"@jupyterlab/observables": ^5.0.0

0 commit comments

Comments
 (0)