Debian Codegraph @

packages/lab-extension/src/index.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 import {
5 ILabShell,
6 JupyterFrontEnd,
7 JupyterFrontEndPlugin,
8 } from '@jupyterlab/application';
9
10 import { ICommandPalette, IToolbarWidgetRegistry } from '@jupyterlab/apputils';
11
12 import { PageConfig, URLExt } from '@jupyterlab/coreutils';
13
14 import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
15
16 import { ITranslator } from '@jupyterlab/translation';
17
18 import { Menu, MenuBar, Widget } from '@lumino/widgets';
19
20 import {
21 defaultNotebookPathOpener,
22 INotebookPathOpener,
23 INotebookShell,
24 } from '@jupyter-notebook/application';
25
26 import {
27 caretDownIcon,
28 CommandToolbarButton,
29 launchIcon,
30 } from '@jupyterlab/ui-components';
31
32 /**
33 * The command IDs used by the application plugin.
34 */
35 namespace CommandIDs {
36 /**
37 * Launch Jupyter Notebook Tree
38 */
39 export const launchNotebookTree = 'jupyter-notebook:launch-tree';
40
41 /**
42 * Open Jupyter Notebook
43 */
44 export const openNotebook = 'jupyter-notebook:open-notebook';
45
46 /**
47 * Open in JupyterLab
48 */
49 export const openLab = 'jupyter-notebook:open-lab';
50
51 /**
52 * Open in NbClassic
53 */
54 export const openNbClassic = 'jupyter-notebook:open-nbclassic';
55 }
56
57 interface ISwitcherChoice {
58 command: string;
59 commandLabel: string;
60 commandDescription: string;
61 buttonLabel: string;
62 urlPrefix: string;
63 }
64
65 /**
66 * A plugin to add custom toolbar items to the notebook page
67 */
68 const interfaceSwitcher: JupyterFrontEndPlugin<void> = {
69 id: '@jupyter-notebook/lab-extension:interface-switcher',
70 description: 'A plugin to add custom toolbar items to the notebook page.',
71 autoStart: true,
72 requires: [ITranslator],
73 optional: [
74 INotebookTracker,
75 ICommandPalette,
76 INotebookPathOpener,
77 INotebookShell,
78 ILabShell,
79 IToolbarWidgetRegistry,
80 ],
81 activate: (
82 app: JupyterFrontEnd,
83 translator: ITranslator,
84 notebookTracker: INotebookTracker | null,
85 palette: ICommandPalette | null,
86 notebookPathOpener: INotebookPathOpener | null,
87 notebookShell: INotebookShell | null,
88 labShell: ILabShell | null,
89 toolbarRegistry: IToolbarWidgetRegistry | null
90 ) => {
91 if (!notebookTracker) {
92 // bail if trying to use this plugin without a notebook tracker
93 return;
94 }
95
96 const { commands, shell } = app;
97 const baseUrl = PageConfig.getBaseUrl();
98 const trans = translator.load('notebook');
99 const nbClassicEnabled =
100 PageConfig.getOption('nbclassic_enabled') === 'true';
101 const switcher = new Menu({ commands });
102 const switcherOptions: ISwitcherChoice[] = [];
103 const opener = notebookPathOpener ?? defaultNotebookPathOpener;
104
105 if (!notebookShell) {
106 switcherOptions.push({
107 command: CommandIDs.openNotebook,
108 commandLabel: trans.__('Notebook'),
109 commandDescription: trans.__('Open in %1', 'Jupyter Notebook'),
110 buttonLabel: 'openNotebook',
111 urlPrefix: `${baseUrl}tree`,
112 });
113 }
114
115 if (!labShell) {
116 switcherOptions.push({
117 command: CommandIDs.openLab,
118 commandLabel: trans.__('JupyterLab'),
119 commandDescription: trans.__('Open in %1', 'JupyterLab'),
120 buttonLabel: 'openLab',
121 urlPrefix: `${baseUrl}doc/tree`,
122 });
123 }
124
125 if (nbClassicEnabled) {
126 switcherOptions.push({
127 command: CommandIDs.openNbClassic,
128 commandLabel: trans.__('NbClassic'),
129 commandDescription: trans.__('Open in %1', 'NbClassic'),
130 buttonLabel: 'openNbClassic',
131 urlPrefix: `${baseUrl}nbclassic/notebooks`,
132 });
133 }
134
135 const isEnabled = () => {
136 return (
137 notebookTracker.currentWidget !== null &&
138 notebookTracker.currentWidget === shell.currentWidget
139 );
140 };
141
142 const addSwitcherCommand = (option: ISwitcherChoice) => {
143 const { command, commandLabel, commandDescription, urlPrefix } = option;
144
145 const execute = () => {
146 const current = notebookTracker.currentWidget;
147 if (!current) {
148 return;
149 }
150 opener.open({
151 prefix: urlPrefix,
152 path: current.context.path,
153 });
154 };
155
156 commands.addCommand(command, {
157 label: (args) => {
158 args.noLabel ? '' : commandLabel;
159 if (args.isMenu || args.isPalette) {
160 return commandDescription;
161 }
162 return commandLabel;
163 },
164 caption: commandLabel,
165 execute,
166 isEnabled,
167 });
168
169 if (palette) {
170 palette.addItem({
171 command,
172 category: 'Other',
173 args: { isPalette: true },
174 });
175 }
176 };
177
178 switcherOptions.forEach((option) => {
179 const { command } = option;
180 addSwitcherCommand(option);
181 switcher.addItem({ command });
182 });
183
184 let toolbarFactory: (panel: NotebookPanel) => Widget;
185 if (switcherOptions.length === 1) {
186 toolbarFactory = (panel: NotebookPanel) => {
187 const toolbarButton = new CommandToolbarButton({
188 commands,
189 id: switcherOptions[0].command,
190 label: switcherOptions[0].commandLabel,
191 icon: launchIcon,
192 });
193 toolbarButton.addClass('jp-nb-interface-switcher-button');
194 return toolbarButton;
195 };
196 } else {
197 const overflowOptions = {
198 overflowMenuOptions: { isVisible: false },
199 };
200 const menubar = new MenuBar(overflowOptions);
201 switcher.title.label = trans.__('Open in...');
202 switcher.title.icon = caretDownIcon;
203 menubar.addMenu(switcher);
204
205 toolbarFactory = (panel: NotebookPanel) => {
206 const menubar = new MenuBar(overflowOptions);
207 menubar.addMenu(switcher);
208 menubar.addClass('jp-InterfaceSwitcher');
209 return menubar;
210 };
211 }
212
213 if (toolbarRegistry) {
214 toolbarRegistry.addFactory<NotebookPanel>(
215 'Notebook',
216 'interfaceSwitcher',
217 toolbarFactory
218 );
219 }
220 },
221 };
222
223 /**
224 * A plugin to add a command to open the Jupyter Notebook Tree.
225 */
226 const launchNotebookTree: JupyterFrontEndPlugin<void> = {
227 id: '@jupyter-notebook/lab-extension:launch-tree',
228 description: 'A plugin to add a command to open the Jupyter Notebook Tree.',
229 autoStart: true,
230 requires: [ITranslator],
231 optional: [ICommandPalette],
232 activate: (
233 app: JupyterFrontEnd,
234 translator: ITranslator,
235 palette: ICommandPalette | null
236 ): void => {
237 const { commands } = app;
238 const trans = translator.load('notebook');
239 const category = trans.__('Help');
240
241 commands.addCommand(CommandIDs.launchNotebookTree, {
242 label: trans.__('Launch Jupyter Notebook File Browser'),
243 execute: () => {
244 const url = URLExt.join(PageConfig.getBaseUrl(), 'tree');
245 window.open(url);
246 },
247 });
248
249 if (palette) {
250 palette.addItem({ command: CommandIDs.launchNotebookTree, category });
251 }
252 },
253 };
254
255 /**
256 * Export the plugins as default.
257 */
258 const plugins: JupyterFrontEndPlugin<any>[] = [
259 launchNotebookTree,
260 interfaceSwitcher,
261 ];
262
263 export default plugins;