ipynb extension need not handle NotebookDocument and NotebookCell

Co-authored-by: rebornix <penn.lv@gmail.com>
This commit is contained in:
Joyce Er 2021-07-23 17:06:46 -07:00
parent a4b8ffb897
commit 4e5d793a78
2 changed files with 11 additions and 23 deletions

View file

@ -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'],
@ -123,15 +123,12 @@ 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);
@ -139,24 +136,23 @@ export function createJupyterCellFromNotebookCell(
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) {
@ -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) {

View file

@ -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);