Skip to content

Instantly share code, notes, and snippets.

@jesuino
Created October 29, 2025 01:51
Show Gist options
  • Select an option

  • Save jesuino/973dd68cbaa2d6aaf39165d321c2f9e6 to your computer and use it in GitHub Desktop.

Select an option

Save jesuino/973dd68cbaa2d6aaf39165d321c2f9e6 to your computer and use it in GitHub Desktop.
diff --git a/labextension/src/lib/CellUtils.ts b/labextension/src/lib/CellUtils.ts
index 2424b76..102b297 100644
--- a/labextension/src/lib/CellUtils.ts
+++ b/labextension/src/lib/CellUtils.ts
@@ -8,8 +8,6 @@ import {
} from '@jupyterlab/nbformat';
import { Notebook, NotebookActions, NotebookPanel } from '@jupyterlab/notebook';
import { PartialJSONObject } from '@lumino/coreutils';
-// Project Components
-import NotebookUtilities from './NotebookUtils';
/** Contains some utility functions for handling notebook cells */
export default class CellUtilities {
@@ -249,91 +247,7 @@ export default class CellUtilities {
notebook.activeCellIndex = notebook.widgets.length - 1;
}
}
-
- /**
- * @description Inserts a cell into the notebook, the new cell will be at the specified index.
- * @param notebook The notebook panel to insert the cell in
- * @param index The index of where the new cell will be inserted.
- * If the cell index is less than or equal to 0, it will be added at the top.
- * If the cell index is greater than the last index, it will be added at the bottom.
- * @returns number - The index it where the cell was inserted
- */
- public static insertCellAtIndex(notebook: Notebook, index: number): number {
- if (!notebook || !notebook.model) {
- throw new Error('Notebook model is null');
- }
-
- // Create a new cell - use different approaches based on available APIs
- let cell: ICellModel;
- const model = notebook.model as any;
- // here I dont know what type to use
- // because contentFactory, sharedModel, createCell are not typed in the notebook model
-
- if (model.contentFactory && typeof model.contentFactory.createCodeCell === 'function') {
- // Old API
- cell = model.contentFactory.createCodeCell({});
- } else if (model.sharedModel && typeof model.sharedModel.createCell === 'function') {
- // New API
- cell = model.sharedModel.createCell('code');
- } else {
- // Fallback - try to create using notebook model methods
- try {
- cell = (notebook.model as any).createCell('code');
- } catch (error) {
- throw new Error('Unable to create new cell: ' + (error || 'unknow'));
- }
- }
-
- // Save the old index, then set the current active cell
- let oldIndex = notebook.activeCellIndex;
-
- // Adjust old index for cells inserted above active cell.
- if (oldIndex >= index) {
- oldIndex += 1;
- }
- const cells = notebook.model.cells as any; // here I dont know what type to use
- if (index <= 0) {
- // Insert at beginning
- if (typeof cells.insert === 'function') {
- cells.insert(0, cell);
- } else if (typeof cells.insertAll === 'function') {
- cells.insertAll(0, [cell]);
- } else {
- // Fallback
- cells.unshift(cell);
- }
- notebook.activeCellIndex = oldIndex;
- return 0;
- }
-
- if (index >= notebook.widgets.length) {
- // Insert at end
- const insertIndex = notebook.widgets.length;
- if (typeof cells.insert === 'function') {
- cells.insert(insertIndex, cell);
- } else if (typeof cells.insertAll === 'function') {
- cells.insertAll(insertIndex, [cell]);
- } else {
- // Fallback
- cells.push(cell);
- }
- notebook.activeCellIndex = oldIndex;
- return insertIndex;
- }
-
- // Insert at specific index
- if (typeof cells.insert === 'function') {
- cells.insert(index, cell);
- } else if (typeof cells.insertAll === 'function') {
- cells.insertAll(index, [cell]);
- } else {
- // Fallback
- cells.splice(index, 0, cell);
- }
- notebook.activeCellIndex = oldIndex;
- return index;
- }
-
+
/**
* @description Injects code into the specified cell of a notebook, does not run the code.
* Warning: the existing cell's code/text will be overwritten.
@@ -377,134 +291,6 @@ export default class CellUtilities {
throw new Error('Cell is not a code cell.');
}
- /**
- * @description This will insert a new cell at the specified index and the inject the specified code into it.
- * @param notebook The notebook to insert the cell into
- * @param index The index of where the new cell will be inserted.
- * If the cell index is less than or equal to 0, it will be added at the top.
- * If the cell index is greater than the last index, it will be added at the bottom.
- * @param code The code to inject into the cell after it has been inserted
- * @returns number - index of where the cell was inserted
- */
- public static insertInjectCode(
- notebook: Notebook,
- index: number,
- code: string,
- ): number {
- const newIndex = CellUtilities.insertCellAtIndex(notebook, index);
- CellUtilities.injectCodeAtIndex(notebook, newIndex, code);
- return newIndex;
- }
-
- /**
- * @description This will insert a new cell at the specified index, inject the specified code into it and the run the code.
- * Note: The code will be run but the results (output or errors) will not be displayed in the cell. Best for void functions.
- * @param notebookPanel The notebook to insert the cell into
- * @param index The index of where the new cell will be inserted and run.
- * If the cell index is less than or equal to 0, it will be added at the top.
- * If the cell index is greater than the last index, it will be added at the bottom.
- * @param code The code to inject into the cell after it has been inserted
- * @param deleteOnError If set to true, the cell will be deleted if the code results in an error
- * @returns Promise<[number, string]> - A promise for when the cell code has executed
- * containing the cell's index and output result
- */
- public static async insertAndRun(
- notebookPanel: NotebookPanel,
- index: number,
- code: string,
- deleteOnError: boolean,
- ): Promise<[number, string]> {
- let insertionIndex: number | undefined;
- try {
- insertionIndex = CellUtilities.insertInjectCode(
- notebookPanel.content,
- index,
- code,
- );
- const output: string = await NotebookUtilities.sendKernelRequestFromNotebook(
- notebookPanel,
- code,
- { output: 'output' },
- false,
- );
- return [insertionIndex, output];
- } catch (error) {
- if (deleteOnError && insertionIndex !== undefined) {
- CellUtilities.deleteCellAtIndex(notebookPanel.content, insertionIndex);
- }
- throw error;
- }
- }
-
- /**
- * @description This will insert a new cell at the specified index, inject the specified code into it and the run the code.
- * Note: The code will be run and the result (output or errors) WILL BE DISPLAYED in the cell.
- * @param notebookPanel The notebook to insert the cell into
- * @param command The command registry which can execute the run command.
- * @param index The index of where the new cell will be inserted and run.
- * If the cell index is less than or equal to 0, it will be added at the top.
- * If the cell index is greater than the last index, it will be added at the bottom.
- * @param code The code to inject into the cell after it has been inserted
- * @param deleteOnError If set to true, the cell will be deleted if the code results in an error
- * @returns Promise<[number, string]> - A promise for when the cell code has executed
- * containing the cell's index and output result
- */
- public static async insertRunShow(
- notebookPanel: NotebookPanel,
- index: number,
- code: string,
- deleteOnError: boolean,
- ): Promise<[number, string]> {
- let insertionIndex: number | undefined;
- try {
- insertionIndex = CellUtilities.insertInjectCode(
- notebookPanel.content,
- index,
- code,
- );
- const output: string = await CellUtilities.runCellAtIndex(
- notebookPanel,
- insertionIndex,
- );
- return [insertionIndex, output];
- } catch (error) {
- if (deleteOnError && insertionIndex !== undefined) {
- CellUtilities.deleteCellAtIndex(notebookPanel.content, insertionIndex);
- }
- throw error;
- }
- }
-
- /**
- * @deprecated Using NotebookUtilities.sendSimpleKernelRequest or NotebookUtilities.sendKernelRequest
- * will execute code directly in the kernel without the need to create a cell and delete it.
- * @description This will insert a cell with specified code at the top and run the code.
- * Once the code is run and output received, the cell is deleted, giving back cell's output.
- * If the code results in an error, the injected cell is still deleted but the promise will be rejected.
- * @param notebookPanel The notebook to run the code in
- * @param code The code to run in the cell
- * @param insertAtEnd True means the cell will be inserted at the bottom
- * @returns Promise<string> - A promise when the cell has been deleted, containing the execution result as a string
- */
- public static async runAndDelete(
- notebookPanel: NotebookPanel,
- code: string,
- insertAtEnd = true,
- ): Promise<string> {
- let idx: number = -1;
- if (insertAtEnd && notebookPanel.content.model) {
- idx = notebookPanel.content.model.cells.length;
- }
- const [index, result]: [number, string] = await CellUtilities.insertAndRun(
- notebookPanel,
- idx,
- code,
- true,
- );
- CellUtilities.deleteCellAtIndex(notebookPanel.content, index);
- return result;
- }
-
public static getStepName(notebook: NotebookPanel, index: number): string {
const names: string[] = (
this.getCellMetaData(notebook.content, index, 'tags') || []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment