From 4f7a2ddf75d3a83277869d343bb71ba6b58e8957 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Fri, 3 Sep 2021 13:17:09 -0700 Subject: [PATCH 1/4] Add setKernelSpecAndLanguageInfo to ipynb ext --- extensions/ipynb/src/ipynbMain.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/extensions/ipynb/src/ipynbMain.ts b/extensions/ipynb/src/ipynbMain.ts index 25aa98af6fcb2..df2a936eac552 100644 --- a/extensions/ipynb/src/ipynbMain.ts +++ b/extensions/ipynb/src/ipynbMain.ts @@ -41,6 +41,26 @@ export function activate(context: vscode.ExtensionContext) { }); return vscode.workspace.applyEdit(edit); }, + setKernelSpecAndLanguageInfo: async (resource: vscode.Uri, kernelspec: unknown, language_info: unknown): Promise => { + const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); + if (!document) { + return false; + } + + const edit = new vscode.WorkspaceEdit(); + edit.replaceNotebookMetadata(resource, { + ...document.metadata, + custom: { + ...(document.metadata.custom ?? {}), + metadata: { + ...(document.metadata.custom?.metadata ?? {}), + kernelspec: kernelspec, + language_info: language_info + }, + } + }); + return vscode.workspace.applyEdit(edit); + }, }; } From 426d60625c8da7082b09d454ef2c179711e17705 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Fri, 3 Sep 2021 14:17:27 -0700 Subject: [PATCH 2/4] Add language_info to setKernelSpec --- extensions/ipynb/src/ipynbMain.ts | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/extensions/ipynb/src/ipynbMain.ts b/extensions/ipynb/src/ipynbMain.ts index df2a936eac552..144ec49209a80 100644 --- a/extensions/ipynb/src/ipynbMain.ts +++ b/extensions/ipynb/src/ipynbMain.ts @@ -22,26 +22,7 @@ export function activate(context: vscode.ExtensionContext) { exportNotebook: (notebook: vscode.NotebookData): string => { return exportNotebook(notebook, serializer); }, - setKernelSpec: async (resource: vscode.Uri, kernelspec: unknown): Promise => { - const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); - if (!document) { - return false; - } - - const edit = new vscode.WorkspaceEdit(); - edit.replaceNotebookMetadata(resource, { - ...document.metadata, - custom: { - ...(document.metadata.custom ?? {}), - metadata: { - ...(document.metadata.custom?.metadata ?? {}), - kernelspec: kernelspec - }, - } - }); - return vscode.workspace.applyEdit(edit); - }, - setKernelSpecAndLanguageInfo: async (resource: vscode.Uri, kernelspec: unknown, language_info: unknown): Promise => { + setKernelSpec: async (resource: vscode.Uri, kernelspec: unknown, language_info?: unknown): Promise => { const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); if (!document) { return false; @@ -55,7 +36,7 @@ export function activate(context: vscode.ExtensionContext) { metadata: { ...(document.metadata.custom?.metadata ?? {}), kernelspec: kernelspec, - language_info: language_info + ...(language_info ? { language_info: language_info } : {}), }, } }); From 55af133ac43df33fefb0f5215785860fefd0c747 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Tue, 7 Sep 2021 09:40:55 -0700 Subject: [PATCH 3/4] Udpates --- extensions/ipynb/src/ipynbMain.ts | 43 ++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/extensions/ipynb/src/ipynbMain.ts b/extensions/ipynb/src/ipynbMain.ts index 144ec49209a80..72c949d736c72 100644 --- a/extensions/ipynb/src/ipynbMain.ts +++ b/extensions/ipynb/src/ipynbMain.ts @@ -6,6 +6,25 @@ import * as vscode from 'vscode'; import { NotebookSerializer } from './notebookSerializer'; +// From {nbformat.INotebookMetadata} in @jupyterlab/coreutils +type NotebookMetadata = { + kernelspec?: { + name: string; + display_name: string; + [propName: string]: unknown; + }; + language_info?: { + name: string; + codemirror_mode?: string | {}; + file_extension?: string; + mimetype?: string; + pygments_lexer?: string; + [propName: string]: unknown; + }; + orig_nbformat: number; + [propName: string]: unknown; +}; + export function activate(context: vscode.ExtensionContext) { const serializer = new NotebookSerializer(context); context.subscriptions.push(vscode.workspace.registerNotebookSerializer('jupyter-notebook', serializer, { @@ -22,7 +41,7 @@ export function activate(context: vscode.ExtensionContext) { exportNotebook: (notebook: vscode.NotebookData): string => { return exportNotebook(notebook, serializer); }, - setKernelSpec: async (resource: vscode.Uri, kernelspec: unknown, language_info?: unknown): Promise => { + setKernelSpec: async (resource: vscode.Uri, kernelspec: unknown): Promise => { const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); if (!document) { return false; @@ -35,8 +54,26 @@ export function activate(context: vscode.ExtensionContext) { ...(document.metadata.custom ?? {}), metadata: { ...(document.metadata.custom?.metadata ?? {}), - kernelspec: kernelspec, - ...(language_info ? { language_info: language_info } : {}), + kernelspec: kernelspec + }, + } + }); + return vscode.workspace.applyEdit(edit); + }, + setNotebookMetadata: async (resource: vscode.Uri, metadata: Partial): Promise => { + const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); + if (!document) { + return false; + } + + const edit = new vscode.WorkspaceEdit(); + edit.replaceNotebookMetadata(resource, { + ...document.metadata, + custom: { + ...(document.metadata.custom ?? {}), + metadata: { + ...(document.metadata.custom?.metadata ?? {}), + ...metadata }, } }); From c962433c68d1583c00b8e60a5448a2a5d580ae46 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Tue, 7 Sep 2021 13:04:03 -0700 Subject: [PATCH 4/4] Remove old API --- extensions/ipynb/src/ipynbMain.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/extensions/ipynb/src/ipynbMain.ts b/extensions/ipynb/src/ipynbMain.ts index 72c949d736c72..5ac60c568e5d7 100644 --- a/extensions/ipynb/src/ipynbMain.ts +++ b/extensions/ipynb/src/ipynbMain.ts @@ -41,25 +41,6 @@ export function activate(context: vscode.ExtensionContext) { exportNotebook: (notebook: vscode.NotebookData): string => { return exportNotebook(notebook, serializer); }, - setKernelSpec: async (resource: vscode.Uri, kernelspec: unknown): Promise => { - const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); - if (!document) { - return false; - } - - const edit = new vscode.WorkspaceEdit(); - edit.replaceNotebookMetadata(resource, { - ...document.metadata, - custom: { - ...(document.metadata.custom ?? {}), - metadata: { - ...(document.metadata.custom?.metadata ?? {}), - kernelspec: kernelspec - }, - } - }); - return vscode.workspace.applyEdit(edit); - }, setNotebookMetadata: async (resource: vscode.Uri, metadata: Partial): Promise => { const document = vscode.workspace.notebookDocuments.find(doc => doc.uri.toString() === resource.toString()); if (!document) {