Skip to content

Commit 336842b

Browse files
authored
Ability to export VS Code Native notebooks (#12879)
1 parent 4355ac2 commit 336842b

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"onCommand:python.viewTestOutput",
6464
"onCommand:python.viewOutput",
6565
"onCommand:python.datascience.viewJupyterOutput",
66+
"onCOmmand:python.datascience.export",
6667
"onCommand:python.datascience.exportAsPythonScript",
6768
"onCommand:python.datascience.exportToHTML",
6869
"onCommand:python.datascience.exportToPDF",
@@ -361,6 +362,15 @@
361362
"title": "%python.command.python.datascience.viewJupyterOutput.title%",
362363
"category": "Python"
363364
},
365+
{
366+
"command": "python.datascience.export",
367+
"title": "%DataScience.notebookExportAs%",
368+
"category": "Python",
369+
"icon": {
370+
"light": "resources/light/export_to_python.svg",
371+
"dark": "resources/dark/export_to_python.svg"
372+
}
373+
},
364374
{
365375
"command": "python.datascience.exportAsPythonScript",
366376
"title": "%python.command.python.datascience.exportAsPythonScript.title%",
@@ -849,6 +859,12 @@
849859
"title": "%python.command.python.datascience.restartkernel.title%",
850860
"group": "navigation",
851861
"when": "notebookEditorFocused"
862+
},
863+
{
864+
"command": "python.datascience.export",
865+
"title": "%DataScience.notebookExportAs%",
866+
"group": "navigation",
867+
"when": "notebookEditorFocused"
852868
}
853869
],
854870
"explorer/context": [
@@ -1236,6 +1252,12 @@
12361252
"title": "%DataScience.gatherQuality%",
12371253
"category": "Python",
12381254
"when": "false"
1255+
},
1256+
{
1257+
"command": "python.datascience.export",
1258+
"title": "%DataScience.notebookExportAs%",
1259+
"category": "Python",
1260+
"when": "false"
12391261
}
12401262
],
12411263
"view/title": [

resources/dark/export_to_python.svg

Lines changed: 4 additions & 0 deletions
Loading

resources/light/export_to_python.svg

Lines changed: 4 additions & 0 deletions
Loading

src/client/common/application/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgu
175175
[DSCommands.ExportAsPythonScript]: [INotebookModel];
176176
[DSCommands.ExportToHTML]: [INotebookModel, string | undefined];
177177
[DSCommands.ExportToPDF]: [INotebookModel, string | undefined];
178-
[DSCommands.Export]: [INotebookModel, string | undefined];
178+
[DSCommands.Export]: [Uri | INotebookModel, string | undefined];
179179
[DSCommands.SwitchJupyterKernel]: [INotebook | undefined, 'raw' | 'jupyter'];
180180
[DSCommands.SelectJupyterCommandLine]: [undefined | Uri];
181181
[DSCommands.SaveNotebookNonCustomEditor]: [Uri];

src/client/datascience/commands/exportCommands.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { QuickPickItem, QuickPickOptions } from 'vscode';
7+
import { QuickPickItem, QuickPickOptions, Uri } from 'vscode';
88
import { getLocString } from '../../../datascience-ui/react-common/locReactSide';
99
import { ICommandNameArgumentTypeMapping } from '../../common/application/commands';
1010
import { IApplicationShell, ICommandManager } from '../../common/application/types';
11+
import { IFileSystem } from '../../common/platform/types';
1112
import { IDisposable } from '../../common/types';
1213
import { DataScience } from '../../common/utils/localize';
14+
import { isUri } from '../../common/utils/misc';
1315
import { sendTelemetryEvent } from '../../telemetry';
1416
import { Commands, Telemetry } from '../constants';
1517
import { ExportManager } from '../export/exportManager';
@@ -27,7 +29,8 @@ export class ExportCommands implements IDisposable {
2729
@inject(ICommandManager) private readonly commandManager: ICommandManager,
2830
@inject(IExportManager) private exportManager: ExportManager,
2931
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell,
30-
@inject(INotebookEditorProvider) private readonly notebookProvider: INotebookEditorProvider
32+
@inject(INotebookEditorProvider) private readonly notebookProvider: INotebookEditorProvider,
33+
@inject(IFileSystem) private readonly fs: IFileSystem
3134
) {}
3235
public register() {
3336
this.registerCommand(Commands.ExportAsPythonScript, (model) => this.export(model, ExportFormat.python));
@@ -55,9 +58,22 @@ export class ExportCommands implements IDisposable {
5558
this.disposables.push(disposable);
5659
}
5760

58-
private async export(model: INotebookModel, exportMethod?: ExportFormat, defaultFileName?: string) {
61+
private async export(modelOrUri: Uri | INotebookModel, exportMethod?: ExportFormat, defaultFileName?: string) {
62+
defaultFileName = typeof defaultFileName === 'string' ? defaultFileName : undefined;
63+
let model: INotebookModel | undefined;
64+
if (modelOrUri && isUri(modelOrUri)) {
65+
const uri = modelOrUri;
66+
const editor = this.notebookProvider.editors.find((item) =>
67+
this.fs.arePathsSame(item.file.fsPath, uri.fsPath)
68+
);
69+
if (editor && editor.model) {
70+
model = editor.model;
71+
}
72+
} else {
73+
model = modelOrUri;
74+
}
5975
if (!model) {
60-
// if no model was passed then this was called from the command pallete,
76+
// if no model was passed then this was called from the command palette,
6177
// so we need to get the active editor
6278
const activeEditor = this.notebookProvider.activeEditor;
6379
if (!activeEditor || !activeEditor.model) {

0 commit comments

Comments
 (0)