-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add a scratch-pad console to the notebook #7790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
brichet
wants to merge
8
commits into
jupyter:main
Choose a base branch
from
brichet:scratchpad-console
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
41256d4
Add a scratch-pad console to the notebook
brichet a156fd8
Toggle the console with shortcut on 'body' -> disable application-ext…
brichet c1fac6d
Add the interaction mode to the console to catch commands
brichet 57b3c12
Use the jupyterlab command to create a console, to also have the cons…
brichet 811f7b5
Open the console in sidepanel only if it shares the kernel with the N…
brichet f0d27c0
Update snapshots
brichet aa4c66b
Do not close an already opened console if the command come from a menu
brichet 7daecc7
update snapshots
brichet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "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": "scratch-pad-console:open", | ||
| "rank": 2, | ||
| "args": { | ||
| "isMenu": true | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| "jupyter.lab.shortcuts": [ | ||
| { | ||
| "command": "scratch-pad-console:open", | ||
| "keys": ["Accel B"], | ||
| "selector": "body" | ||
| } | ||
| ], | ||
| "properties": {}, | ||
| "additionalProperties": false, | ||
| "type": "object" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,17 +7,40 @@ import { | |
| JupyterFrontEndPlugin, | ||
| } from '@jupyterlab/application'; | ||
|
|
||
| import { ICommandPalette } from '@jupyterlab/apputils'; | ||
|
|
||
| import { IConsoleTracker } from '@jupyterlab/console'; | ||
|
|
||
| import { PageConfig, URLExt } from '@jupyterlab/coreutils'; | ||
|
|
||
| import { INotebookTracker } from '@jupyterlab/notebook'; | ||
|
|
||
| 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 { ReadonlyJSONObject } from '@lumino/coreutils'; | ||
|
|
||
| import { Widget } from '@lumino/widgets'; | ||
|
|
||
| const COMMANDS_TO_PATCH = [ | ||
| 'console:clear', | ||
| 'console:run-unforced', | ||
| 'console:run-forced', | ||
| 'console:linebreak', | ||
| 'console:interrupt-kernel', | ||
| 'console:restart-kernel', | ||
| 'console:change-kernel', | ||
| ]; | ||
|
|
||
| /** | ||
| * A plugin to open consoles in a new tab | ||
| */ | ||
|
|
@@ -53,23 +76,58 @@ const opener: JupyterFrontEndPlugin<void> = { | |
| }; | ||
|
|
||
| /** | ||
| * Open consoles in a new tab. | ||
| * Open consoles in a new tab or in the side panel (scratch-pad like). | ||
| */ | ||
| const redirect: JupyterFrontEndPlugin<void> = { | ||
| id: '@jupyter-notebook/console-extension:redirect', | ||
| requires: [IConsoleTracker], | ||
| optional: [INotebookPathOpener], | ||
| optional: [INotebookPathOpener, INotebookShell, INotebookTracker], | ||
| autoStart: true, | ||
| description: 'Open consoles in a new tab', | ||
| activate: ( | ||
| app: JupyterFrontEnd, | ||
| tracker: IConsoleTracker, | ||
| notebookPathOpener: INotebookPathOpener | null | ||
| notebookPathOpener: INotebookPathOpener | null, | ||
| notebookShell: INotebookShell | null, | ||
| notebookTracker: INotebookTracker | null | ||
| ) => { | ||
| const baseUrl = PageConfig.getBaseUrl(); | ||
| const opener = notebookPathOpener ?? defaultNotebookPathOpener; | ||
|
|
||
| tracker.widgetAdded.connect(async (send, console) => { | ||
| // Check if we should open the console in side panel: | ||
| // - this is a notebook view | ||
| // - the notebook and the console share the same kernel | ||
| // Otherwise, the console opens in a new tab. | ||
| if (notebookShell && notebookTracker) { | ||
| const notebook = notebookTracker.currentWidget; | ||
|
|
||
| // Wait for the notebook and console to be ready. | ||
| await Promise.all([ | ||
| notebook?.sessionContext.ready, | ||
| console.sessionContext.ready, | ||
| ]); | ||
| const notebookKernelId = notebook?.sessionContext.session?.kernel?.id; | ||
| const consoleKernelId = console.sessionContext.session?.kernel?.id; | ||
|
|
||
| if (notebookKernelId === consoleKernelId) { | ||
| notebookShell.add(console, 'right'); | ||
| notebookShell.expandRight(console.id); | ||
|
|
||
| // Some commands needs to be patched to be enabled. | ||
| // TODO: fix it upstream in jupyterlab. | ||
| app.commands | ||
| .listCommands() | ||
| .filter((id) => COMMANDS_TO_PATCH.includes(id)) | ||
| .forEach((id) => { | ||
| (app.commands as any)._commands.get(id).isEnabled = () => | ||
| tracker.currentWidget !== null && | ||
| tracker.currentWidget.node.contains(document.activeElement); | ||
| }); | ||
|
Comment on lines
+119
to
+126
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This patch updates the |
||
| return; | ||
| } | ||
| } | ||
|
|
||
| const { sessionContext } = console; | ||
| await sessionContext.ready; | ||
| const widget = find( | ||
|
|
@@ -92,9 +150,121 @@ const redirect: JupyterFrontEndPlugin<void> = { | |
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Open consoles in the side panel. | ||
| */ | ||
| const scratchPadConsole: JupyterFrontEndPlugin<void> = { | ||
| id: '@jupyter-notebook/console-extension:scratch-pad', | ||
| requires: [INotebookTracker], | ||
| optional: [INotebookShell, ICommandPalette, ITranslator], | ||
| autoStart: true, | ||
| description: 'Open consoles in a new tab', | ||
| activate: ( | ||
| app: JupyterFrontEnd, | ||
| 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 = 'scratch-pad-console:open'; | ||
| 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 (args) => { | ||
| if (!notebookShell) { | ||
| return; | ||
| } | ||
| const consoleId = scratchPadConsole.id; | ||
| const sidebar = notebookShell.rightHandler; | ||
|
|
||
| // Close the console if it is already opened (shortcut only). | ||
| if (sidebar.isVisible && sidebar.currentWidget?.id === consoleId) { | ||
| if (!args.isPalette && !args.isMenu) { | ||
| notebookShell.collapseRight(); | ||
| notebookShell.currentWidget?.activate(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| let panel: Widget | undefined = sidebar.widgets.find( | ||
| (w) => w.id === consoleId | ||
| ); | ||
|
|
||
| // 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 = await commands.execute('console:create', { | ||
| kernelPreference: { ...kernelPref, id } as ReadonlyJSONObject, | ||
| }); | ||
|
|
||
| if (!panel) { | ||
| console.log('An error occurred during console widget creation'); | ||
| return; | ||
| } | ||
|
|
||
| panel.title.caption = trans.__('Console'); | ||
| panel.id = consoleId; | ||
| } else { | ||
| notebookShell.expandRight(consoleId); | ||
| } | ||
| }, | ||
| describedBy: { | ||
| args: { | ||
| type: 'object', | ||
| properties: { | ||
| isPalette: { | ||
| type: 'boolean', | ||
| description: trans.__( | ||
| trans.__('Whether the command is executed from the palette') | ||
| ), | ||
| }, | ||
| isMenu: { | ||
| type: 'boolean', | ||
| description: trans.__( | ||
| trans.__('Whether the command is executed from the menu') | ||
| ), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| 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; | ||
Binary file modified
BIN
+810 Bytes
(110%)
ui-tests/test/menus.spec.ts-snapshots/opened-menu-file-new-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.29 KB
(130%)
ui-tests/test/menus.spec.ts-snapshots/opened-menu-file-new-firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extension is not enabled anymore, except for the
treeview, because it already uses theCtrl + Bshortcut.AFAIK the commands in this extension may not be necessary for the Notebook view (not sure it is necessary at all, most of them requires a
LabShellto work).