Skip to content

Commit 877f22c

Browse files
Add custom commands that use "memory" terminology instead of the word "kernel" (#156)
* Add a `jupytereverywhere:restart-memory` command * Add `jupytereverywhere:restart-and-run-all` * Add comments to describe both commands * "Kernel restart failed" ➡️ "Memory restart failed" * Fix path to `fast-forward.svg` * Add tests for both commands * Update Playwright Snapshots --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 22a0642 commit 877f22c

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

schema/plugin.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@
5858
},
5959
{
6060
"name": "restart",
61-
"caption": "Restart notebook"
61+
"command": "jupytereverywhere:restart-memory",
62+
"caption": "Restart notebook",
63+
"label": ""
6264
},
6365
{
6466
"name": "restart-and-run",
65-
"caption": "Restart and run all cells"
67+
"caption": "Restart and run all cells",
68+
"command": "jupytereverywhere:restart-and-run-all"
6669
},
6770
{
6871
"name": "share",

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export namespace Commands {
66
export const downloadPDFCommand = 'jupytereverywhere:download-pdf';
77
export const shareNotebookCommand = 'jupytereverywhere:share-notebook';
88
export const createCopyNotebookCommand = 'jupytereverywhere:create-copy-notebook';
9+
export const restartMemoryCommand = 'jupytereverywhere:restart-memory';
10+
export const restartMemoryAndRunAllCommand = 'jupytereverywhere:restart-and-run-all';
911
}

src/index.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { SharingService } from './sharing-service';
99

1010
import { createSuccessDialog, createErrorDialog } from './ui-components/share-dialog';
1111

12+
import { LabIcon } from '@jupyterlab/ui-components';
13+
import refreshIcon from '../style/icons/refresh.svg';
14+
import fastForwardSvg from '../style/icons/fast-forward.svg';
15+
1216
import { exportNotebookAsPDF } from './pdf';
1317
import { files } from './pages/files';
1418
import { Commands } from './commands';
@@ -211,6 +215,75 @@ const plugin: JupyterFrontEndPlugin<void> = {
211215
}
212216
});
213217

218+
/**
219+
* Add a command to restart the notebook kernel, terming it as "memory"
220+
*/
221+
const RefreshLabIcon = new LabIcon({
222+
name: 'jupytereverywhere:refresh',
223+
svgstr: refreshIcon
224+
});
225+
226+
commands.addCommand(Commands.restartMemoryCommand, {
227+
label: 'Restart Notebook Memory',
228+
icon: RefreshLabIcon,
229+
execute: async () => {
230+
const panel = tracker.currentWidget;
231+
if (!panel) {
232+
console.warn('No active notebook to restart.');
233+
return;
234+
}
235+
236+
const result = await showDialog({
237+
title: 'Would you like to restart the notebook’s memory?',
238+
buttons: [Dialog.cancelButton({ label: 'Cancel' }), Dialog.okButton({ label: 'Restart' })]
239+
});
240+
241+
if (result.button.accept) {
242+
try {
243+
await panel.sessionContext.restartKernel();
244+
} catch (err) {
245+
console.error('Memory restart failed', err);
246+
}
247+
}
248+
}
249+
});
250+
251+
/**
252+
* Add a command to restart the notebook kernel, terming it as "memory",
253+
* and run all cells after the restart.
254+
*/
255+
const customFastForwardIcon = new LabIcon({
256+
name: 'jupytereverywhere:restart-run',
257+
svgstr: fastForwardSvg
258+
});
259+
260+
commands.addCommand(Commands.restartMemoryAndRunAllCommand, {
261+
label: 'Restart Notebook Memory and Run All Cells',
262+
icon: customFastForwardIcon,
263+
isEnabled: () => !!tracker.currentWidget,
264+
execute: async () => {
265+
const panel = tracker.currentWidget;
266+
if (!panel) {
267+
console.warn('No active notebook to restart and run.');
268+
return;
269+
}
270+
271+
const result = await showDialog({
272+
title: 'Would you like to restart the notebook’s memory and rerun all cells?',
273+
buttons: [Dialog.cancelButton({ label: 'Cancel' }), Dialog.okButton({ label: 'Restart' })]
274+
});
275+
276+
if (result.button.accept) {
277+
try {
278+
await panel.sessionContext.restartKernel();
279+
await commands.execute('notebook:run-all-cells');
280+
} catch (err) {
281+
console.error('Restarting and running all cells failed', err);
282+
}
283+
}
284+
}
285+
});
286+
214287
/**
215288
* Add custom Share notebook command
216289
*/

ui-tests/tests/jupytereverywhere.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,27 @@ test.describe('Title of the pages should be "Jupyter Everywhere"', () => {
559559
expect(title).toBe('Jupyter Everywhere');
560560
});
561561
});
562+
563+
test.describe('Kernel commands should use memory terminology', () => {
564+
test('Restart memory command', async ({ page }) => {
565+
const promise = runCommand(page, 'jupytereverywhere:restart-memory');
566+
const dialog = page.locator('.jp-Dialog-content');
567+
568+
await expect(dialog).toBeVisible();
569+
expect(await dialog.screenshot()).toMatchSnapshot('restart-memory-dialog.png');
570+
571+
await dialog.press('Escape');
572+
await promise;
573+
});
574+
575+
test('Restart memory and run all cells command', async ({ page }) => {
576+
const promise = runCommand(page, 'jupytereverywhere:restart-and-run-all');
577+
const dialog = page.locator('.jp-Dialog-content');
578+
579+
await expect(dialog).toBeVisible();
580+
expect(await dialog.screenshot()).toMatchSnapshot('restart-memory-run-all-dialog.png');
581+
582+
await dialog.press('Escape');
583+
await promise;
584+
});
585+
});
7.86 KB
Loading
8.56 KB
Loading

0 commit comments

Comments
 (0)