Add a scratch-pad console to the notebook

pull/7790/head
Nicolas Brichet 4 months ago
parent 9836de423c
commit 41256d4c8a

@ -40,9 +40,16 @@
"dependencies": {
"@jupyter-notebook/application": "^7.6.0-alpha.0",
"@jupyterlab/application": "~4.6.0-alpha.0",
"@jupyterlab/apputils": "~4.7.0-alpha.0",
"@jupyterlab/codeeditor": "~4.6.0-alpha.0",
"@jupyterlab/console": "~4.6.0-alpha.0",
"@jupyterlab/coreutils": "~6.6.0-alpha.0",
"@lumino/algorithm": "^2.0.4"
"@jupyterlab/notebook": "~4.6.0-alpha.0",
"@jupyterlab/rendermime": "~4.6.0-alpha.0",
"@jupyterlab/translation": "~4.6.0-alpha.0",
"@jupyterlab/ui-components": "~4.6.0-alpha.0",
"@lumino/algorithm": "^2.0.4",
"@lumino/widgets": "^2.7.2"
},
"devDependencies": {
"rimraf": "^3.0.2",
@ -52,7 +59,8 @@
"access": "public"
},
"jupyterlab": {
"extension": true
"extension": true,
"schemaDir": "schema"
},
"styleModule": "style/index.js"
}

@ -0,0 +1,35 @@
{
"title": "Jupyter Notebook Scratchpad Console",
"description": "Jupyter Notebook Scratchpad Console",
"jupyter.lab.menus": {
"main": [
{
"id": "jp-mainmenu-file",
"items": [
{
"type": "submenu",
"submenu": {
"id": "jp-mainmenu-file-new",
"items": [
{
"command": "console:scratch-pad",
"rank": 2
}
]
}
}
]
}
]
},
"jupyter.lab.shortcuts": [
{
"command": "console:scratch-pad",
"keys": ["Accel B"],
"selector": ".jp-Notebook"
}
],
"properties": {},
"additionalProperties": false,
"type": "object"
}

@ -7,17 +7,36 @@ import {
JupyterFrontEndPlugin,
} from '@jupyterlab/application';
import { IConsoleTracker } from '@jupyterlab/console';
import { ICommandPalette } from '@jupyterlab/apputils';
import { IEditorServices } from '@jupyterlab/codeeditor';
import {
ConsolePanel,
IConsoleCellExecutor,
IConsoleTracker,
} from '@jupyterlab/console';
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
import { INotebookTracker } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { consoleIcon } from '@jupyterlab/ui-components';
import {
INotebookPathOpener,
INotebookShell,
defaultNotebookPathOpener,
} from '@jupyter-notebook/application';
import { find } from '@lumino/algorithm';
import { Widget } from '@lumino/widgets';
/**
* A plugin to open consoles in a new tab
*/
@ -92,9 +111,114 @@ const redirect: JupyterFrontEndPlugin<void> = {
},
};
/**
* Open consoles in the side panel.
*/
const scratchPadConsole: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/console-extension:scratch-pad',
requires: [
ConsolePanel.IContentFactory,
IConsoleCellExecutor,
IEditorServices,
IRenderMimeRegistry,
INotebookTracker,
],
optional: [INotebookShell, ICommandPalette, ITranslator],
autoStart: true,
description: 'Open consoles in a new tab',
activate: (
app: JupyterFrontEnd,
contentFactory: ConsolePanel.IContentFactory,
executor: IConsoleCellExecutor,
editorServices: IEditorServices,
rendermime: IRenderMimeRegistry,
tracker: INotebookTracker,
notebookShell: INotebookShell | null,
palette: ICommandPalette | null,
translator: ITranslator | null
) => {
const { commands } = app;
const manager = app.serviceManager;
const trans = (translator ?? nullTranslator).load('notebook');
const command = 'console:scratch-pad';
commands.addCommand(command, {
label: (args) =>
args['isPalette']
? trans.__('Open a scratch-pad console')
: trans.__('Scratch-pad console'),
isVisible: () => !!tracker.currentWidget,
icon: (args) => (args['isPalette'] ? undefined : consoleIcon),
execute: async () => {
if (!notebookShell) {
return;
}
let panel: Widget | undefined = notebookShell.rightHandler.widgets.find(
(w) => w.id === scratchPadConsole.id
);
// Create the widget if it is not already in the right area.
if (!panel) {
const notebook = tracker.currentWidget;
if (!notebook) {
return;
}
const notebookSessionContext = notebook.sessionContext;
await Promise.all([notebookSessionContext.ready, manager.ready]);
const id = notebookSessionContext.session?.kernel?.id;
const kernelPref = notebookSessionContext.kernelPreference;
panel = new ConsolePanel({
manager,
contentFactory,
mimeTypeService: editorServices.mimeTypeService,
rendermime,
executor,
kernelPreference: { ...kernelPref, id },
});
panel.title.caption = trans.__('Console');
panel.id = scratchPadConsole.id;
app.shell.add(panel, 'right');
}
notebookShell.expandRight(scratchPadConsole.id);
},
describedBy: {
args: {
type: 'object',
properties: {
isPalette: {
type: 'boolean',
description: trans.__(
trans.__('Whether the command is executed from the palette')
),
},
},
},
},
});
if (palette) {
palette.addItem({
category: 'Notebook Console',
command,
args: { isPalette: true },
});
}
},
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [opener, redirect];
const plugins: JupyterFrontEndPlugin<any>[] = [
opener,
redirect,
scratchPadConsole,
];
export default plugins;

@ -2383,8 +2383,14 @@ __metadata:
dependencies:
"@jupyter-notebook/application": ^7.6.0-alpha.0
"@jupyterlab/application": ~4.6.0-alpha.0
"@jupyterlab/apputils": ~4.7.0-alpha.0
"@jupyterlab/codeeditor": ~4.6.0-alpha.0
"@jupyterlab/console": ~4.6.0-alpha.0
"@jupyterlab/coreutils": ~6.6.0-alpha.0
"@jupyterlab/notebook": ~4.6.0-alpha.0
"@jupyterlab/rendermime": ~4.6.0-alpha.0
"@jupyterlab/translation": ~4.6.0-alpha.0
"@jupyterlab/ui-components": ~4.6.0-alpha.0
"@lumino/algorithm": ^2.0.4
rimraf: ^3.0.2
typescript: ~5.5.4

Loading…
Cancel
Save