123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- Menu
- } from 'phosphor/lib/ui/menu';
- import {
- JupyterLab, JupyterLabPlugin
- } from '../application';
- import {
- IClipboard
- } from '../clipboard';
- import {
- IEditorServices
- } from '../codeeditor';
- import {
- ICommandPalette
- } from '../commandpalette';
- import {
- IMainMenu
- } from '../mainmenu';
- import {
- IDocumentRegistry,
- restartKernel, selectKernelForContext
- } from '../docregistry';
- import {
- IInspector
- } from '../inspector';
- import {
- ILayoutRestorer
- } from '../layoutrestorer';
- import {
- IRenderMime
- } from '../rendermime';
- import {
- IServiceManager
- } from '../services';
- import {
- INotebookTracker, NotebookModelFactory, NotebookPanel, NotebookTracker,
- NotebookWidgetFactory, NotebookActions, Notebook
- } from './index';
- /**
- * The class name for all main area portrait tab icons.
- */
- const PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon';
- /**
- * The class name for the notebook icon from the default theme.
- */
- const NOTEBOOK_ICON_CLASS = 'jp-ImageNotebook';
- /**
- * The map of command ids used by the notebook.
- */
- const cmdIds = {
- interrupt: 'notebook:interrupt-kernel',
- restart: 'notebook:restart-kernel',
- restartClear: 'notebook:restart-clear',
- restartRunAll: 'notebook:restart-runAll',
- switchKernel: 'notebook:switch-kernel',
- clearAllOutputs: 'notebook:clear-outputs',
- closeAndHalt: 'notebook:close-and-halt',
- run: 'notebook-cells:run',
- runAndAdvance: 'notebook-cells:run-and-advance',
- runAndInsert: 'notebook-cells:run-and-insert',
- runAll: 'notebook:run-all',
- toCode: 'notebook-cells:to-code',
- toMarkdown: 'notebook-cells:to-markdown',
- toRaw: 'notebook-cells:to-raw',
- cut: 'notebook-cells:cut',
- copy: 'notebook-cells:copy',
- paste: 'notebook-cells:paste',
- moveUp: 'notebook-cells:move-up',
- moveDown: 'notebook-cells:move-down',
- clearOutputs: 'notebook-cells:clear-output',
- deleteCell: 'notebook-cells:delete',
- insertAbove: 'notebook-cells:insert-above',
- insertBelow: 'notebook-cells:insert-below',
- selectAbove: 'notebook-cells:select-above',
- selectBelow: 'notebook-cells:select-below',
- extendAbove: 'notebook-cells:extend-above',
- extendBelow: 'notebook-cells:extend-below',
- editMode: 'notebook:edit-mode',
- merge: 'notebook-cells:merge',
- split: 'notebook-cells:split',
- commandMode: 'notebook:command-mode',
- toggleLines: 'notebook-cells:toggle-line-numbers',
- toggleAllLines: 'notebook-cells:toggle-all-line-numbers',
- undo: 'notebook-cells:undo',
- redo: 'notebook-cells:redo',
- markdown1: 'notebook-cells:markdown-header1',
- markdown2: 'notebook-cells:markdown-header2',
- markdown3: 'notebook-cells:markdown-header3',
- markdown4: 'notebook-cells:markdown-header4',
- markdown5: 'notebook-cells:markdown-header5',
- markdown6: 'notebook-cells:markdown-header6',
- };
- /**
- * The name of the factory that creates notebooks.
- */
- const FACTORY = 'Notebook';
- /**
- * The notebook widget tracker provider.
- */
- export
- const trackerPlugin: JupyterLabPlugin<INotebookTracker> = {
- id: 'jupyter.services.notebook-tracker',
- provides: INotebookTracker,
- requires: [
- IDocumentRegistry,
- IServiceManager,
- IRenderMime,
- IClipboard,
- IMainMenu,
- ICommandPalette,
- IInspector,
- NotebookPanel.IRenderer,
- ILayoutRestorer
- ],
- activate: activateNotebookHandler,
- autoStart: true
- };
- /**
- * The notebook renderer provider.
- */
- export
- const rendererPlugin: JupyterLabPlugin<NotebookPanel.IRenderer> = {
- id: 'jupyter.services.notebook-renderer',
- provides: NotebookPanel.IRenderer,
- requires: [IEditorServices],
- autoStart: true,
- activate: (app: JupyterLab, editorServices: IEditorServices) => {
- const notebookRenderer = new Notebook.Renderer({ editorServices });
- return new NotebookPanel.Renderer({ notebookRenderer });
- }
- };
- /**
- * Activate the notebook handler extension.
- */
- function activateNotebookHandler(app: JupyterLab, registry: IDocumentRegistry, services: IServiceManager, rendermime: IRenderMime, clipboard: IClipboard, mainMenu: IMainMenu, palette: ICommandPalette, inspector: IInspector, renderer: NotebookPanel.IRenderer, layout: ILayoutRestorer): INotebookTracker {
- const factory = new NotebookWidgetFactory({
- name: FACTORY,
- fileExtensions: ['.ipynb'],
- modelName: 'notebook',
- defaultFor: ['.ipynb'],
- preferKernel: true,
- canStartKernel: true,
- rendermime,
- clipboard,
- renderer
- });
- const tracker = new NotebookTracker({ namespace: 'notebook' });
- // Handle state restoration.
- layout.restore(tracker, {
- command: 'file-operations:open',
- args: panel => ({ path: panel.context.path, factory: FACTORY }),
- name: panel => panel.context.path,
- when: services.ready
- });
- // Sync tracker and set the source of the code inspector.
- app.shell.currentChanged.connect((sender, args) => {
- let widget = tracker.sync(args.newValue);
- if (widget) {
- inspector.source = widget.content.inspectionHandler;
- }
- });
- registry.addModelFactory(new NotebookModelFactory());
- registry.addWidgetFactory(factory);
- registry.addFileType({
- name: 'Notebook',
- extension: '.ipynb',
- contentType: 'notebook',
- fileFormat: 'json'
- });
- registry.addCreator({
- name: 'Notebook',
- fileType: 'Notebook',
- widgetName: 'Notebook'
- });
- addCommands(app, services, tracker);
- populatePalette(palette);
- let id = 0; // The ID counter for notebook panels.
- factory.widgetCreated.connect((sender, widget) => {
- // If the notebook panel does not have an ID, assign it one.
- widget.id = widget.id || `notebook-${++id}`;
- widget.title.icon = `${PORTRAIT_ICON_CLASS} ${NOTEBOOK_ICON_CLASS}`;
- // Immediately set the inspector source to the current notebook.
- inspector.source = widget.content.inspectionHandler;
- // Notify the instance tracker if restore data needs to update.
- widget.context.pathChanged.connect(() => { tracker.save(widget); });
- // Add the notebook panel to the tracker.
- tracker.add(widget);
- });
- // Add main menu notebook menu.
- mainMenu.addMenu(createMenu(app), { rank: 20 });
- return tracker;
- }
- /**
- * Add the notebook commands to the application's command registry.
- */
- function addCommands(app: JupyterLab, services: IServiceManager, tracker: NotebookTracker): void {
- let commands = app.commands;
- commands.addCommand(cmdIds.runAndAdvance, {
- label: 'Run Cell(s) and Advance',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- let content = current.content;
- NotebookActions.runAndAdvance(content, current.context.kernel);
- }
- }
- });
- commands.addCommand(cmdIds.run, {
- label: 'Run Cell(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.run(current.content, current.context.kernel);
- }
- }
- });
- commands.addCommand(cmdIds.runAndInsert, {
- label: 'Run Cell(s) and Insert',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.runAndInsert(current.content, current.context.kernel);
- }
- }
- });
- commands.addCommand(cmdIds.runAll, {
- label: 'Run All Cells',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.runAll(current.content, current.context.kernel);
- }
- }
- });
- commands.addCommand(cmdIds.restart, {
- label: 'Restart Kernel',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- restartKernel(current.kernel, current.node).then(() => {
- current.activate();
- });
- }
- }
- });
- commands.addCommand(cmdIds.closeAndHalt, {
- label: 'Close and Halt',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- current.context.changeKernel(null).then(() => { current.dispose(); });
- }
- }
- });
- commands.addCommand(cmdIds.restartClear, {
- label: 'Restart Kernel & Clear Outputs',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- let promise = restartKernel(current.kernel, current.node);
- promise.then(result => {
- current.activate();
- if (result) {
- NotebookActions.clearAllOutputs(current.content);
- }
- });
- }
- }
- });
- commands.addCommand(cmdIds.restartRunAll, {
- label: 'Restart Kernel & Run All',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- let promise = restartKernel(current.kernel, current.node);
- promise.then(result => {
- current.activate();
- NotebookActions.runAll(current.content, current.context.kernel);
- });
- }
- }
- });
- commands.addCommand(cmdIds.clearAllOutputs, {
- label: 'Clear All Outputs',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.clearAllOutputs(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.clearOutputs, {
- label: 'Clear Output(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.clearOutputs(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.interrupt, {
- label: 'Interrupt Kernel',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- let kernel = current.context.kernel;
- if (kernel) {
- kernel.interrupt();
- }
- }
- }
- });
- commands.addCommand(cmdIds.toCode, {
- label: 'Convert to Code',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.changeCellType(current.content, 'code');
- }
- }
- });
- commands.addCommand(cmdIds.toMarkdown, {
- label: 'Convert to Markdown',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.changeCellType(current.content, 'markdown');
- }
- }
- });
- commands.addCommand(cmdIds.toRaw, {
- label: 'Convert to Raw',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.changeCellType(current.content, 'raw');
- }
- }
- });
- commands.addCommand(cmdIds.cut, {
- label: 'Cut Cell(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.cut(current.content, current.clipboard);
- }
- }
- });
- commands.addCommand(cmdIds.copy, {
- label: 'Copy Cell(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.copy(current.content, current.clipboard);
- }
- }
- });
- commands.addCommand(cmdIds.paste, {
- label: 'Paste Cell(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.paste(current.content, current.clipboard);
- }
- }
- });
- commands.addCommand(cmdIds.deleteCell, {
- label: 'Delete Cell(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.deleteCells(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.split, {
- label: 'Split Cell',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.splitCell(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.merge, {
- label: 'Merge Selected Cell(s)',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.mergeCells(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.insertAbove, {
- label: 'Insert Cell Above',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.insertAbove(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.insertBelow, {
- label: 'Insert Cell Below',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.insertBelow(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.selectAbove, {
- label: 'Select Cell Above',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.selectAbove(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.selectBelow, {
- label: 'Select Cell Below',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.selectBelow(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.extendAbove, {
- label: 'Extend Selection Above',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.extendSelectionAbove(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.extendBelow, {
- label: 'Extend Selection Below',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.extendSelectionBelow(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.moveUp, {
- label: 'Move Cell(s) Up',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.moveUp(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.moveDown, {
- label: 'Move Cell(s) Down',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.moveDown(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.toggleLines, {
- label: 'Toggle Line Numbers',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.toggleLineNumbers(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.toggleAllLines, {
- label: 'Toggle All Line Numbers',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.toggleAllLineNumbers(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.commandMode, {
- label: 'To Command Mode',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- current.content.mode = 'command';
- }
- }
- });
- commands.addCommand(cmdIds.editMode, {
- label: 'To Edit Mode',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- current.content.mode = 'edit';
- }
- }
- });
- commands.addCommand(cmdIds.undo, {
- label: 'Undo Cell Operation',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.undo(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.redo, {
- label: 'Redo Cell Operation',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.redo(current.content);
- }
- }
- });
- commands.addCommand(cmdIds.switchKernel, {
- label: 'Switch Kernel',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- let context = current.context;
- let node = current.node;
- selectKernelForContext(context, services.sessions, node).then(() => {
- current.activate();
- });
- }
- }
- });
- commands.addCommand(cmdIds.markdown1, {
- label: 'Markdown Header 1',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.setMarkdownHeader(current.content, 1);
- }
- }
- });
- commands.addCommand(cmdIds.markdown2, {
- label: 'Markdown Header 2',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.setMarkdownHeader(current.content, 2);
- }
- }
- });
- commands.addCommand(cmdIds.markdown3, {
- label: 'Markdown Header 3',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.setMarkdownHeader(current.content, 3);
- }
- }
- });
- commands.addCommand(cmdIds.markdown4, {
- label: 'Markdown Header 4',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.setMarkdownHeader(current.content, 4);
- }
- }
- });
- commands.addCommand(cmdIds.markdown5, {
- label: 'Markdown Header 5',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.setMarkdownHeader(current.content, 5);
- }
- }
- });
- commands.addCommand(cmdIds.markdown6, {
- label: 'Markdown Header 6',
- execute: () => {
- let current = tracker.currentWidget;
- if (current) {
- NotebookActions.setMarkdownHeader(current.content, 6);
- }
- }
- });
- }
- /**
- * Populate the application's command palette with notebook commands.
- */
- function populatePalette(palette: ICommandPalette): void {
- let category = 'Notebook Operations';
- [
- cmdIds.interrupt,
- cmdIds.restart,
- cmdIds.restartClear,
- cmdIds.restartRunAll,
- cmdIds.runAll,
- cmdIds.clearAllOutputs,
- cmdIds.toggleAllLines,
- cmdIds.editMode,
- cmdIds.commandMode,
- cmdIds.switchKernel,
- cmdIds.closeAndHalt
- ].forEach(command => { palette.addItem({ command, category }); });
- category = 'Notebook Cell Operations';
- [
- cmdIds.run,
- cmdIds.runAndAdvance,
- cmdIds.runAndInsert,
- cmdIds.clearOutputs,
- cmdIds.toCode,
- cmdIds.toMarkdown,
- cmdIds.toRaw,
- cmdIds.cut,
- cmdIds.copy,
- cmdIds.paste,
- cmdIds.deleteCell,
- cmdIds.split,
- cmdIds.merge,
- cmdIds.insertAbove,
- cmdIds.insertBelow,
- cmdIds.selectAbove,
- cmdIds.selectBelow,
- cmdIds.extendAbove,
- cmdIds.extendBelow,
- cmdIds.moveDown,
- cmdIds.moveUp,
- cmdIds.toggleLines,
- cmdIds.undo,
- cmdIds.redo,
- cmdIds.markdown1,
- cmdIds.markdown2,
- cmdIds.markdown3,
- cmdIds.markdown4,
- cmdIds.markdown5,
- cmdIds.markdown6
- ].forEach(command => { palette.addItem({ command, category }); });
- }
- /**
- * Creates a menu for the notebook.
- */
- function createMenu(app: JupyterLab): Menu {
- let { commands, keymap } = app;
- let menu = new Menu({ commands, keymap });
- let settings = new Menu({ commands, keymap });
- menu.title.label = 'Notebook';
- settings.title.label = 'Settings';
- settings.addItem({ command: cmdIds.toggleAllLines });
- menu.addItem({ command: cmdIds.undo });
- menu.addItem({ command: cmdIds.redo });
- menu.addItem({ command: cmdIds.split });
- menu.addItem({ command: cmdIds.deleteCell });
- menu.addItem({ command: cmdIds.clearAllOutputs });
- menu.addItem({ command: cmdIds.runAll });
- menu.addItem({ command: cmdIds.restart });
- menu.addItem({ command: cmdIds.switchKernel });
- menu.addItem({ command: cmdIds.closeAndHalt });
- menu.addItem({ type: 'separator' });
- menu.addItem({ type: 'submenu', menu: settings });
- return menu;
- }
|