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