How to use the @jupyterlab/cells.isCodeCellModel function in @jupyterlab/cells

To help you get started, we’ve selected a few @jupyterlab/cells examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kubeflow-kale / kale / labextension / src / components / CellTags.tsx View on Github external
//   - stateChanged signal: That is fired only in a few specific circumstances,
            //  like when the trusted or readonly state changes
            //   - contentChange: this is wat we *would* expect to work EXCEPT in this instance.
            //      This is because they internally implement changing a cell type by removing the
            //      old cell and inserting a new one with the same text content.
            //      So you can’t listen to change signals on the old one as it is not really the same cell.
            //  Possible solution:
            //    1. Listen to the model.cells.changed signal.
            //    2. If a cell is deleted, cache the text content and type of the cell.
            //    3. If a cell is subsequently inserted, check to see if it is a new cell
            //       type with the same text content. That is your changed cell.
            //    4. If there is any other action, clear the cache.
            this.props.activeCell.model.contentChanged.connect(this.listenCellContentChanged);

            // if the active cell is not of type `code`, then hide panel
            if (!isCodeCellModel(this.props.notebook.content.model.cells.get(this.props.activeCellIndex))) {
                this.setState({show: false});
                return
            }

            this.readAndShowMetadata();
        }
    };
github kubeflow-kale / kale / labextension / src / lib / NotebookUtils.tsx View on Github external
public static async runGlobalCells(
    notebook: NotebookPanel,
  ): Promise {
    let cell = { cell: notebook.content.widgets[0], index: 0 };
    for (let i = 0; i < notebook.model.cells.length; i++) {
      if (!isCodeCellModel(notebook.model.cells.get(i))) {
        continue;
      }
      const blockName = CellUtilities.getStepName(notebook, i);
      // If a cell of that type is found, run that
      // and all consequent cells getting merged to that one
      if (blockName !== 'skip' && RESERVED_CELL_NAMES.includes(blockName)) {
        while (i < notebook.model.cells.length) {
          if (!isCodeCellModel(notebook.model.cells.get(i))) {
            i++;
            continue;
          }
          const cellName = CellUtilities.getStepName(notebook, i);
          if (cellName !== blockName && cellName !== '') {
            break;
          }
          cell = { cell: notebook.content.widgets[i], index: i };
          this.selectAndScrollToCell(notebook, cell);
          // this.setState({ activeCellIndex: cell.index, activeCell: cell.cell });
          const kernelMsg = (await CodeCell.execute(
            notebook.content.widgets[i] as CodeCell,
            notebook.sessionContext,
          )) as KernelMessage.IExecuteReplyMsg;
          if (kernelMsg.content && kernelMsg.content.status === 'error') {
            return {
github kubeflow-kale / kale / labextension / src / lib / CellUtils.ts View on Github external
public static injectCodeAtIndex(
    notebook: Notebook,
    index: number,
    code: string,
  ): void {
    if (notebook === null) {
      throw new Error('Notebook was null or undefined.');
    }
    if (index < 0 || index >= notebook.model.cells.length) {
      throw new Error('Cell index out of range.');
    }
    const cell: ICellModel = notebook.model.cells.get(index);
    if (isCodeCellModel(cell)) {
      cell.value.text = code;
      return;
    }
    throw new Error('Cell is not a code cell.');
  }
github kubeflow-kale / kale / labextension / src / components / CellTags.tsx View on Github external
componentDidMount = () => {
        if (this.props.activeCellIndex) {
            if (isCodeCellModel(this.props.notebook.content.model.cells.get(this.props.activeCellIndex))) {
                this.readAndShowMetadata();
            }
        }
    };
github jupyterlab / jupyterlab / packages / console / src / widget.ts View on Github external
each(this._cells, cell => {
      let model = cell.model;
      if (isCodeCellModel(model)) {
        cells.push(model.toJSON());
      }
    });
github jupyterlab / jupyterlab-data-explorer / jupyterlab / packages / console / src / widget.ts View on Github external
each(this._cells, cell => {
      let model = cell.model;
      if (isCodeCellModel(model)) {
        cells.push(model.toJSON());
      }
    });
github kubeflow-kale / kale / labextension / src / widgets / cell-metadata / CellMetadataEditor.tsx View on Github external
hideEditorIfNotCodeCell() {
    if (this.props.notebook && !this.props.notebook.isDisposed) {
      const cellModel = this.props.notebook.model.cells.get(
        this.context.activeCellIndex,
      );
      if (!isCodeCellModel(cellModel) && this.context.isEditorVisible) {
        this.closeEditor();
      }
    }
  }
github kubeflow-kale / kale / labextension / src / lib / NotebookUtils.tsx View on Github external
public static clearCellOutputs(notebook: NotebookPanel): void {
    for (let i = 0; i < notebook.model.cells.length; i++) {
      if (!isCodeCellModel(notebook.model.cells.get(i))) {
        continue;
      }
      (notebook.model.cells.get(i) as CodeCellModel).executionCount = null;
      (notebook.model.cells.get(i) as CodeCellModel).outputs.clear();
    }
  }
github kubeflow-kale / kale / labextension / src / widgets / cell-metadata / InlineCellMetadata.tsx View on Github external
generateEditorsPropsAndInlineMetadata = () => {
    if (!this.props.notebook) {
      return;
    }
    const metadata: any[] = [];
    const editors: Editors = {};
    const cells = this.props.notebook.model.cells;
    for (let index = 0; index < cells.length; index++) {
      const isCodeCell = isCodeCellModel(
        this.props.notebook.model.cells.get(index),
      );
      if (!isCodeCell) {
        continue;
      }

      let tags = TagsUtils.getKaleCellTags(this.props.notebook.content, index);
      if (!tags) {
        tags = {
          blockName: '',
          prevBlockNames: [],
        };
      }
      let previousBlockName = '';

      if (!tags.blockName) {
github kubeflow-kale / kale / labextension / src / lib / CellUtils.ts View on Github external
public static readOutput(notebook: Notebook, index: number): any {
    if (!notebook) {
      throw new Error('Notebook was null!');
    }
    if (index < 0 || index >= notebook.model.cells.length) {
      throw new Error('Cell index out of range.');
    }
    const cell: ICellModel = notebook.model.cells.get(index);
    if (!isCodeCellModel(cell)) {
      throw new Error('cell is not a code cell.');
    }
    if (cell.outputs.length < 1) {
      return null;
    }
    const out = cell.outputs.toJSON().pop();
    if (isExecuteResult(out)) {
      return out.data['text/plain'];
    }
    if (isStream(out)) {
      return out.text;
    }
    if (isError(out)) {
      const errData: IError = out;

      throw new Error(