UNPKG

jupyterlab-emrys

Version:

A computational environment for Jupyter. Powered by Emrys

809 lines (808 loc) 26.3 kB
// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. "use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var jupyter_js_services_1 = require('jupyter-js-services'); var phosphor_dragdrop_1 = require('phosphor-dragdrop'); var phosphor_menus_1 = require('phosphor-menus'); var plugin_1 = require('../mainmenu/plugin'); var docregistry_1 = require('../docregistry'); var inspector_1 = require('../inspector'); var rendermime_1 = require('../rendermime'); var widgettracker_1 = require('../widgettracker'); var index_1 = require('./index'); /** * The class name for all main area portrait tab icons. */ var PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon'; /** * The class name for the notebook icon from the default theme. */ var NOTEBOOK_ICON_CLASS = 'jp-ImageNotebook'; /** * The map of command ids used by the notebook. */ var 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', 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', 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', }; /** * A class that tracks notebook widgets. */ var NotebookTracker = (function (_super) { __extends(NotebookTracker, _super); function NotebookTracker() { _super.apply(this, arguments); } return NotebookTracker; }(widgettracker_1.WidgetTracker)); exports.NotebookTracker = NotebookTracker; /** * Used to access the application/command palette */ var currentApp; /** * The notebook file handler extension. */ exports.notebookHandlerExtension = { id: 'jupyter.extensions.notebook-handler', requires: [ docregistry_1.DocumentRegistry, jupyter_js_services_1.ServiceManager, rendermime_1.RenderMime, phosphor_dragdrop_1.MimeData, plugin_1.MainMenu, inspector_1.Inspector ], activate: activateNotebookHandler }; /** * The notebook widget tracker provider. */ exports.notebookTrackerProvider = { id: 'jupyter.plugins.notebook-tracker', provides: NotebookTracker, resolve: function () { return Private.notebookTracker; } }; /** * Activate the notebook handler extension. */ function activateNotebookHandler(app, registry, services, rendermime, clipboard, mainMenu, inspector) { var widgetFactory = new index_1.NotebookWidgetFactory(rendermime, clipboard); var options = { fileExtensions: ['.ipynb'], displayName: 'Notebook', modelName: 'notebook', defaultFor: ['.ipynb'], preferKernel: true, canStartKernel: true }; registry.addModelFactory(new index_1.NotebookModelFactory()); registry.addWidgetFactory(widgetFactory, options); // Add the ability to launch notebooks for each kernel type. var displayNameMap = Object.create(null); var specs = services.kernelspecs; for (var kernelName in specs.kernelspecs) { var displayName = specs.kernelspecs[kernelName].spec.display_name; displayNameMap[displayName] = kernelName; } var displayNames = Object.keys(displayNameMap).sort(function (a, b) { return a.localeCompare(b); }); registry.addFileType({ name: 'Notebook', extension: '.ipynb', fileType: 'notebook', fileFormat: 'json' }); for (var _i = 0, displayNames_1 = displayNames; _i < displayNames_1.length; _i++) { var displayName = displayNames_1[_i]; registry.addCreator({ name: displayName + " Notebook", fileType: 'Notebook', widgetName: 'Notebook', kernelName: displayNameMap[displayName] }); } // Track the current active notebook. var tracker = Private.notebookTracker; widgetFactory.widgetCreated.connect(function (sender, widget) { widget.title.icon = PORTRAIT_ICON_CLASS + " " + NOTEBOOK_ICON_CLASS; tracker.addWidget(widget); }); // Set the source of the code inspector to the current console. tracker.activeWidgetChanged.connect(function (sender, panel) { inspector.source = panel.content.inspectionHandler; }); // Add a MainMenu notebook item var notebookMenu = new phosphor_menus_1.MenuItem({ text: 'Notebook', submenu: makeNbMenu(app) }); var menuOptions = { 'rank': 20 }; mainMenu.addItem(notebookMenu, menuOptions); currentApp = app; app.commands.add([ { id: cmdIds.runAndAdvance, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.runAndAdvance(nbWidget.content, nbWidget.context.kernel); } } }, { id: cmdIds.run, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.run(nbWidget.content, nbWidget.context.kernel); } } }, { id: cmdIds.runAndInsert, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.runAndInsert(nbWidget.content, nbWidget.context.kernel); } } }, { id: cmdIds.runAll, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.runAll(nbWidget.content, nbWidget.context.kernel); } } }, { id: cmdIds.restart, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; docregistry_1.restartKernel(nbWidget.kernel, nbWidget.node); } } }, { id: cmdIds.restartClear, handler: function () { if (tracker.activeWidget) { var nbWidget_1 = tracker.activeWidget; var promise = docregistry_1.restartKernel(nbWidget_1.kernel, nbWidget_1.node); promise.then(function (result) { if (result) { index_1.NotebookActions.clearAllOutputs(nbWidget_1.content); } }); } } }, { id: cmdIds.restartRunAll, handler: function () { if (tracker.activeWidget) { var nbWidget_2 = tracker.activeWidget; var promise = docregistry_1.restartKernel(nbWidget_2.kernel, nbWidget_2.node); promise.then(function (result) { index_1.NotebookActions.runAll(nbWidget_2.content, nbWidget_2.context.kernel); }); } } }, { id: cmdIds.clearAllOutputs, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.clearAllOutputs(nbWidget.content); } } }, { id: cmdIds.clearOutputs, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.clearOutputs(nbWidget.content); } } }, { id: cmdIds.interrupt, handler: function () { if (tracker.activeWidget) { var kernel = tracker.activeWidget.context.kernel; if (kernel) { kernel.interrupt(); } } } }, { id: cmdIds.toCode, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.changeCellType(nbWidget.content, 'code'); } } }, { id: cmdIds.toMarkdown, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.changeCellType(nbWidget.content, 'markdown'); } } }, { id: cmdIds.toRaw, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.changeCellType(nbWidget.content, 'raw'); } } }, { id: cmdIds.cut, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.cut(nbWidget.content, nbWidget.clipboard); } } }, { id: cmdIds.copy, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.copy(nbWidget.content, nbWidget.clipboard); } } }, { id: cmdIds.paste, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.paste(nbWidget.content, nbWidget.clipboard); } } }, { id: cmdIds.deleteCell, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.deleteCells(nbWidget.content); } } }, { id: cmdIds.split, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.splitCell(nbWidget.content); } } }, { id: cmdIds.merge, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.mergeCells(nbWidget.content); } } }, { id: cmdIds.insertAbove, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.insertAbove(nbWidget.content); } } }, { id: cmdIds.insertBelow, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.insertBelow(nbWidget.content); } } }, { id: cmdIds.selectAbove, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.selectAbove(nbWidget.content); } } }, { id: cmdIds.selectBelow, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.selectBelow(nbWidget.content); } } }, { id: cmdIds.extendAbove, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.extendSelectionAbove(nbWidget.content); } } }, { id: cmdIds.extendBelow, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.extendSelectionBelow(nbWidget.content); } } }, { id: cmdIds.toggleLines, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.toggleLineNumbers(nbWidget.content); } } }, { id: cmdIds.toggleAllLines, handler: function () { if (tracker.activeWidget) { var nbWidget = tracker.activeWidget; index_1.NotebookActions.toggleAllLineNumbers(nbWidget.content); } } }, { id: cmdIds.commandMode, handler: function () { if (tracker.activeWidget) { tracker.activeWidget.content.mode = 'command'; } } }, { id: cmdIds.editMode, handler: function () { if (tracker.activeWidget) { tracker.activeWidget.content.mode = 'edit'; } } }, { id: cmdIds.undo, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.undo(tracker.activeWidget.content); } } }, { id: cmdIds.redo, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.redo(tracker.activeWidget.content); } } }, { id: cmdIds.switchKernel, handler: function () { if (tracker.activeWidget) { docregistry_1.selectKernelForContext(tracker.activeWidget.context, tracker.activeWidget.node); } } }, { id: cmdIds.markdown1, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.setMarkdownHeader(tracker.activeWidget.content, 1); } } }, { id: cmdIds.markdown2, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.setMarkdownHeader(tracker.activeWidget.content, 2); } } }, { id: cmdIds.markdown3, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.setMarkdownHeader(tracker.activeWidget.content, 3); } } }, { id: cmdIds.markdown4, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.setMarkdownHeader(tracker.activeWidget.content, 4); } } }, { id: cmdIds.markdown5, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.setMarkdownHeader(tracker.activeWidget.content, 5); } } }, { id: cmdIds.markdown6, handler: function () { if (tracker.activeWidget) { index_1.NotebookActions.setMarkdownHeader(tracker.activeWidget.content, 6); } } } ]); app.palette.add([ { command: cmdIds.run, category: 'Notebook Cell Operations', text: 'Run Cell(s)' }, { command: cmdIds.runAndAdvance, category: 'Notebook Cell Operations', text: 'Run Cell(s) and Advance' }, { command: cmdIds.runAndInsert, category: 'Notebook Cell Operations', text: 'Run Cell(s) and Insert' }, { command: cmdIds.interrupt, category: 'Notebook Operations', text: 'Interrupt Kernel' }, { command: cmdIds.restart, category: 'Notebook Operations', text: 'Restart Kernel' }, { command: cmdIds.restartClear, category: 'Notebook Operations', text: 'Restart Kernel & Clear Outputs' }, { command: cmdIds.restartRunAll, category: 'Notebook Operations', text: 'Restart Kernel & Run All' }, { command: cmdIds.runAll, category: 'Notebook Operations', text: 'Run All Cells' }, { command: cmdIds.clearAllOutputs, category: 'Notebook Operations', text: 'Clear All Outputs' }, { command: cmdIds.clearOutputs, category: 'Notebook Cell Operations', text: 'Clear Output(s)' }, { command: cmdIds.toCode, category: 'Notebook Cell Operations', text: 'Convert to Code' }, { command: cmdIds.toMarkdown, category: 'Notebook Cell Operations', text: 'Convert to Markdown' }, { command: cmdIds.toRaw, category: 'Notebook Cell Operations', text: 'Convert to Raw' }, { command: cmdIds.cut, category: 'Notebook Cell Operations', text: 'Cut Cell(s)' }, { command: cmdIds.copy, category: 'Notebook Cell Operations', text: 'Copy Cell(s)' }, { command: cmdIds.paste, category: 'Notebook Cell Operations', text: 'Paste Cell(s)' }, { command: cmdIds.deleteCell, category: 'Notebook Cell Operations', text: 'Delete Cell(s)' }, { command: cmdIds.split, category: 'Notebook Cell Operations', text: 'Split Cell' }, { command: cmdIds.merge, category: 'Notebook Cell Operations', text: 'Merge Selected Cell(s)' }, { command: cmdIds.insertAbove, category: 'Notebook Cell Operations', text: 'Insert Cell Above' }, { command: cmdIds.insertBelow, category: 'Notebook Cell Operations', text: 'Insert Cell Below' }, { command: cmdIds.selectAbove, category: 'Notebook Cell Operations', text: 'Select Cell Above' }, { command: cmdIds.selectBelow, category: 'Notebook Cell Operations', text: 'Select Cell Below' }, { command: cmdIds.extendAbove, category: 'Notebook Cell Operations', text: 'Extend Selection Above' }, { command: cmdIds.extendBelow, category: 'Notebook Cell Operations', text: 'Extend Selection Below' }, { command: cmdIds.toggleLines, category: 'Notebook Cell Operations', text: 'Toggle Line Numbers' }, { command: cmdIds.toggleAllLines, category: 'Notebook Operations', text: 'Toggle All Line Numbers' }, { command: cmdIds.editMode, category: 'Notebook Operations', text: 'To Edit Mode' }, { command: cmdIds.commandMode, category: 'Notebook Operations', text: 'To Command Mode' }, { command: cmdIds.switchKernel, category: 'Notebook Operations', text: 'Switch Kernel' }, { command: cmdIds.undo, category: 'Notebook Cell Operations', text: 'Undo Cell Operation' }, { command: cmdIds.redo, category: 'Notebook Cell Operations', text: 'Redo Cell Operation' }, { command: cmdIds.markdown1, category: 'Notebook Cell Operations', text: 'Markdown Header 1' }, { command: cmdIds.markdown2, category: 'Notebook Cell Operations', text: 'Markdown Header 2' }, { command: cmdIds.markdown3, category: 'Notebook Cell Operations', text: 'Markdown Header 3' }, { command: cmdIds.markdown4, category: 'Notebook Cell Operations', text: 'Markdown Header 4' }, { command: cmdIds.markdown5, category: 'Notebook Cell Operations', text: 'Markdown Header 5' }, { command: cmdIds.markdown6, category: 'Notebook Cell Operations', text: 'Markdown Header 6' } ]); } /** * Creates a menu item for the notebook */ function makeNbMenu(app) { var settings = new phosphor_menus_1.Menu([ new phosphor_menus_1.MenuItem({ text: 'Toggle line numbers', handler: lineNumberHandler }) ]); var menu = new phosphor_menus_1.Menu([ new phosphor_menus_1.MenuItem({ text: 'New Notebook', handler: function () { app.commands.execute('file-operations:new-notebook'); } }), new phosphor_menus_1.MenuItem({ text: 'Undo', handler: undoHandler }), new phosphor_menus_1.MenuItem({ text: 'Redo', handler: redoHandler }), new phosphor_menus_1.MenuItem({ text: 'Split cell', handler: splitCellHandler }), new phosphor_menus_1.MenuItem({ text: 'Delete cell', handler: deleteCellHandler }), new phosphor_menus_1.MenuItem({ text: 'Clear all outputs', handler: clearOutputHandler }), new phosphor_menus_1.MenuItem({ text: 'Run all cells', handler: runAllHandler }), new phosphor_menus_1.MenuItem({ text: 'Restart kernel', handler: restartKernelHandler }), new phosphor_menus_1.MenuItem({ text: 'Switch kernel', handler: changeKernelHandler }), new phosphor_menus_1.MenuItem({ type: phosphor_menus_1.MenuItem.Separator }), new phosphor_menus_1.MenuItem({ text: 'Settings', submenu: settings }) ]); return menu; } /** * Handler functions for the notebook MainMenu item */ function clearOutputHandler() { currentApp.commands.execute('notebook:clear-outputs'); } function runAllHandler() { currentApp.commands.execute('notebook:run-all'); } function changeKernelHandler() { currentApp.commands.execute('notebook:switch-kernel'); } function lineNumberHandler() { currentApp.commands.execute('notebook-cells:toggle-all-line-numbers'); } function undoHandler() { currentApp.commands.execute('notebook-cells:undo'); } function redoHandler() { currentApp.commands.execute('notebook-cells:redo'); } function deleteCellHandler() { currentApp.commands.execute('notebook-cells:delete'); } function splitCellHandler() { currentApp.commands.execute('notebook-cells:split'); } function commandModeHandler() { currentApp.commands.execute('notebook:command-mode'); } function editModeHandler() { currentApp.commands.execute('notebook:edit-mode'); } function restartKernelHandler() { currentApp.commands.execute('notebook:restart-kernel'); } /** * A namespace for private data. */ var Private; (function (Private) { /** * A singleton instance of a notebook tracker. */ Private.notebookTracker = new NotebookTracker(); })(Private || (Private = {}));