Skip to content

Commit

Permalink
ipynb extension need not handle NotebookDocument and NotebookCell
Browse files Browse the repository at this point in the history
Co-authored-by: rebornix <penn.lv@gmail.com>
  • Loading branch information
joyceerhl and rebornix committed Jul 24, 2021
1 parent a4b8ffb commit 4e5d793
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
22 changes: 9 additions & 13 deletions extensions/ipynb/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { nbformat } from '@jupyterlab/coreutils';
import { extensions, NotebookCell, NotebookCellData, NotebookCellExecutionSummary, NotebookCellKind, NotebookCellOutput, NotebookCellOutputItem, NotebookData } from 'vscode';
import { extensions, NotebookCellData, NotebookCellExecutionSummary, NotebookCellKind, NotebookCellOutput, NotebookCellOutputItem, NotebookData } from 'vscode';

const jupyterLanguageToMonacoLanguageMapping = new Map([
['c#', 'csharp'],
Expand Down Expand Up @@ -123,40 +123,36 @@ function convertJupyterOutputToBuffer(mime: string, value: unknown): NotebookCel
}

export function createJupyterCellFromNotebookCell(
vscCell: NotebookCell | NotebookCellData
vscCell: NotebookCellData
): nbformat.IRawCell | nbformat.IMarkdownCell | nbformat.ICodeCell {
let cell: nbformat.IRawCell | nbformat.IMarkdownCell | nbformat.ICodeCell;
if (vscCell.kind === NotebookCellKind.Markup) {
cell = createMarkdownCellFromNotebookCell(vscCell);
} else if (
('document' in vscCell && vscCell.document.languageId === 'raw') ||
('languageId' in vscCell && vscCell.languageId === 'raw')
) {
} else if (vscCell.languageId === 'raw') {
cell = createRawCellFromNotebookCell(vscCell);
} else {
cell = createCodeCellFromNotebookCell(vscCell);
}
return cell;
}

function createCodeCellFromNotebookCell(cell: NotebookCell | NotebookCellData): nbformat.ICodeCell {
function createCodeCellFromNotebookCell(cell: NotebookCellData): nbformat.ICodeCell {
const cellMetadata = cell.metadata?.custom as CellMetadata | undefined;
const code = 'document' in cell ? cell.document.getText() : cell.value;
const codeCell: any = {
cell_type: 'code',
execution_count: cell.executionSummary?.executionOrder ?? null,
source: splitMultilineString(code),
source: splitMultilineString(cell.value),
outputs: (cell.outputs || []).map(translateCellDisplayOutput),
metadata: cellMetadata?.metadata || {} // This cannot be empty.
};
return codeCell;
}

function createRawCellFromNotebookCell(cell: NotebookCell | NotebookCellData): nbformat.IRawCell {
function createRawCellFromNotebookCell(cell: NotebookCellData): nbformat.IRawCell {
const cellMetadata = cell.metadata?.custom as CellMetadata | undefined;
const rawCell: any = {
cell_type: 'raw',
source: splitMultilineString('document' in cell ? cell.document.getText() : cell.value),
source: splitMultilineString(cell.value),
metadata: cellMetadata?.metadata || {} // This cannot be empty.
};
if (cellMetadata?.attachments) {
Expand Down Expand Up @@ -407,11 +403,11 @@ function convertOutputMimeToJupyterOutput(mime: string, value: Uint8Array) {
}
}

function createMarkdownCellFromNotebookCell(cell: NotebookCell | NotebookCellData): nbformat.IMarkdownCell {
function createMarkdownCellFromNotebookCell(cell: NotebookCellData): nbformat.IMarkdownCell {
const cellMetadata = cell.metadata?.custom as CellMetadata | undefined;
const markdownCell: any = {
cell_type: 'markdown',
source: splitMultilineString('document' in cell ? cell.document.getText() : cell.value),
source: splitMultilineString(cell.value),
metadata: cellMetadata?.metadata || {} // This cannot be empty.
};
if (cellMetadata?.attachments) {
Expand Down
12 changes: 2 additions & 10 deletions extensions/ipynb/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,18 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
return data;
}

public serializeNotebookDocument(data: vscode.NotebookDocument): string {
return this.serialize(data);
}

public serializeNotebook(data: vscode.NotebookData, _token: vscode.CancellationToken): Uint8Array {
return new TextEncoder().encode(this.serialize(data));
}

private serialize(data: vscode.NotebookDocument | vscode.NotebookData): string {
private serialize(data: vscode.NotebookData): string {
const notebookContent: Partial<nbformat.INotebookContent> = data.metadata?.custom || {};
notebookContent.cells = notebookContent.cells || [];
notebookContent.nbformat = notebookContent.nbformat || 4;
notebookContent.nbformat_minor = notebookContent.nbformat_minor || 2;
notebookContent.metadata = notebookContent.metadata || { orig_nbformat: 4 };

const cells = 'notebookType' in data ?
data.getCells() :
data.cells;

notebookContent.cells = cells
notebookContent.cells = data.cells
.map(cell => createJupyterCellFromNotebookCell(cell))
.map(pruneCell);

Expand Down

0 comments on commit 4e5d793

Please sign in to comment.