- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3
Add a "Stop" (⏹️) button to interrupt cell execution #212
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
base: main
Are you sure you want to change the base?
Changes from all commits
f61c52d
              49979f2
              ab8978e
              cf9447c
              0283bda
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|  | @@ -40,13 +40,16 @@ import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application' | |||||||||||||||||||||||||||||||
| import { IEditorServices } from '@jupyterlab/codeeditor'; | ||||||||||||||||||||||||||||||||
| import { ToolbarButton } from '@jupyterlab/ui-components'; | ||||||||||||||||||||||||||||||||
| import { Widget, PanelLayout } from '@lumino/widgets'; | ||||||||||||||||||||||||||||||||
| import { Notebook, NotebookPanel } from '@jupyterlab/notebook'; | ||||||||||||||||||||||||||||||||
| import { Notebook, NotebookPanel, NotebookActions } from '@jupyterlab/notebook'; | ||||||||||||||||||||||||||||||||
| import { EverywhereIcons } from './icons'; | ||||||||||||||||||||||||||||||||
| import { Cell, CodeCell, ICellModel } from '@jupyterlab/cells'; | ||||||||||||||||||||||||||||||||
| import { Message } from '@lumino/messaging'; | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| const INPUT_PROMPT_CLASS = 'jp-InputPrompt'; | ||||||||||||||||||||||||||||||||
| const INPUT_AREA_PROMPT_INDICATOR_CLASS = 'jp-InputArea-prompt-indicator'; | ||||||||||||||||||||||||||||||||
| const INPUT_AREA_PROMPT_INDICATOR_EMPTY_CLASS = 'jp-InputArea-prompt-indicator-empty'; | ||||||||||||||||||||||||||||||||
| const INPUT_AREA_PROMPT_RUN_CLASS = 'jp-InputArea-prompt-run'; | ||||||||||||||||||||||||||||||||
| const INPUT_AREA_PROMPT_STOP_CLASS = 'jp-InputArea-prompt-stop'; | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| export interface IInputPromptIndicator extends Widget { | ||||||||||||||||||||||||||||||||
| executionCount: string | null; | ||||||||||||||||||||||||||||||||
|  | @@ -84,6 +87,8 @@ export class JEInputPrompt extends Widget implements IInputPrompt { | |||||||||||||||||||||||||||||||
| private _customExecutionCount: string | null = null; | ||||||||||||||||||||||||||||||||
| private _promptIndicator: InputPromptIndicator; | ||||||||||||||||||||||||||||||||
| private _runButton: ToolbarButton; | ||||||||||||||||||||||||||||||||
| private _stopButton: ToolbarButton; | ||||||||||||||||||||||||||||||||
| private _ownerCell: CodeCell | null = null; | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| constructor(private _app: JupyterFrontEnd) { | ||||||||||||||||||||||||||||||||
| super(); | ||||||||||||||||||||||||||||||||
|  | @@ -92,6 +97,7 @@ export class JEInputPrompt extends Widget implements IInputPrompt { | |||||||||||||||||||||||||||||||
| const layout = (this.layout = new PanelLayout()); | ||||||||||||||||||||||||||||||||
| this._promptIndicator = new InputPromptIndicator(); | ||||||||||||||||||||||||||||||||
| layout.addWidget(this._promptIndicator); | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| this._runButton = new ToolbarButton({ | ||||||||||||||||||||||||||||||||
| icon: EverywhereIcons.runCell, | ||||||||||||||||||||||||||||||||
| onClick: () => { | ||||||||||||||||||||||||||||||||
|  | @@ -102,6 +108,110 @@ export class JEInputPrompt extends Widget implements IInputPrompt { | |||||||||||||||||||||||||||||||
| this._runButton.addClass(INPUT_AREA_PROMPT_RUN_CLASS); | ||||||||||||||||||||||||||||||||
| this._runButton.addClass('je-cell-run-button'); | ||||||||||||||||||||||||||||||||
| layout.addWidget(this._runButton); | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| this._stopButton = new ToolbarButton({ | ||||||||||||||||||||||||||||||||
| icon: EverywhereIcons.stopCell, | ||||||||||||||||||||||||||||||||
| onClick: async () => { | ||||||||||||||||||||||||||||||||
| const panel = this._app.shell.currentWidget; | ||||||||||||||||||||||||||||||||
| if (!(panel instanceof NotebookPanel)) { | ||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||
| const kernel = panel.sessionContext.session?.kernel; | ||||||||||||||||||||||||||||||||
| if (kernel && typeof kernel.interrupt === 'function') { | ||||||||||||||||||||||||||||||||
| await kernel.interrupt(); | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| await panel.sessionContext.restartKernel(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||
| console.warn('Failed to stop execution (interrupt/restart):', err); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| 
      Comment on lines
    
      +126
     to 
      +128
    
   
     | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| tooltip: 'Stop running this cell' | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| this._stopButton.addClass(INPUT_AREA_PROMPT_STOP_CLASS); | ||||||||||||||||||||||||||||||||
| this._stopButton.addClass('je-cell-stop-button'); | ||||||||||||||||||||||||||||||||
| layout.addWidget(this._stopButton); | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| this._applyPointerEvents(false); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Make sure the correct button is clickable; we disable pointer events for | ||||||||||||||||||||||||||||||||
| * the hidden one to avoid the "wrong tooltip" and accidental clicks. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| private _applyPointerEvents(isExecuting: boolean) { | ||||||||||||||||||||||||||||||||
| const runEl = this._runButton.node; | ||||||||||||||||||||||||||||||||
| const stopEl = this._stopButton.node; | ||||||||||||||||||||||||||||||||
| if (isExecuting) { | ||||||||||||||||||||||||||||||||
| runEl.style.pointerEvents = 'none'; | ||||||||||||||||||||||||||||||||
| stopEl.style.pointerEvents = 'auto'; | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| runEl.style.pointerEvents = 'auto'; | ||||||||||||||||||||||||||||||||
| stopEl.style.pointerEvents = 'none'; | ||||||||||||||||||||||||||||||||
| 
      Comment on lines
    
      +146
     to 
      +151
    
   
     | ||||||||||||||||||||||||||||||||
| if (isExecuting) { | |
| runEl.style.pointerEvents = 'none'; | |
| stopEl.style.pointerEvents = 'auto'; | |
| } else { | |
| runEl.style.pointerEvents = 'auto'; | |
| stopEl.style.pointerEvents = 'none'; | |
| // Remove both classes first to ensure a clean state | |
| runEl.classList.remove('je-pointer-events-none', 'je-pointer-events-auto'); | |
| stopEl.classList.remove('je-pointer-events-none', 'je-pointer-events-auto'); | |
| if (isExecuting) { | |
| runEl.classList.add('je-pointer-events-none'); | |
| stopEl.classList.add('je-pointer-events-auto'); | |
| } else { | |
| runEl.classList.add('je-pointer-events-auto'); | |
| stopEl.classList.add('je-pointer-events-none'); | 
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.
Falling back to kernel restart when interrupt is unavailable may be too aggressive and could cause data loss. Consider providing user confirmation before restarting the kernel, or implement a more graceful degradation strategy.
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.
Yes, makes sense and I'm aware – I've added because interrupts don't really work.