Skip to content

Commit eb6bb49

Browse files
authored
Include notebook renderers in extension (#12793)
1 parent d029820 commit eb6bb49

File tree

4 files changed

+61
-84
lines changed

4 files changed

+61
-84
lines changed

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ gulp.task('webpack', async () => {
177177
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-notebooks.config.js', 'production');
178178
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-viewers.config.js', 'production');
179179
await buildWebPackForDevOrProduction('./build/webpack/webpack.extension.config.js', 'extension');
180+
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-renderers.config.js', 'production');
180181
});
181182

182183
gulp.task('updateBuildNumber', async () => {

package.json

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"languageServerVersion": "0.5.30",
1010
"publisher": "ms-python",
11-
"enableProposedApi": false,
11+
"enableProposedApi": true,
1212
"author": {
1313
"name": "Microsoft Corporation"
1414
},
@@ -3085,7 +3085,43 @@
30853085
"when": "testsDiscovered"
30863086
}
30873087
]
3088-
}
3088+
},
3089+
"notebookOutputRenderer": [
3090+
{
3091+
"viewType": "jupyter-notebook-renderer",
3092+
"displayName": "Jupyter Notebook Renderer",
3093+
"mimeTypes": [
3094+
"application/geo+json",
3095+
"application/vdom.v1+json",
3096+
"application/vnd.dataresource+json",
3097+
"application/vnd.plotly.v1+json",
3098+
"application/vnd.vega.v2+json",
3099+
"application/vnd.vega.v3+json",
3100+
"application/vnd.vega.v4+json",
3101+
"application/vnd.vega.v5+json",
3102+
"application/vnd.vegalite.v1+json",
3103+
"application/vnd.vegalite.v2+json",
3104+
"application/vnd.vegalite.v3+json",
3105+
"application/vnd.vegalite.v4+json",
3106+
"application/x-nteract-model-debug+json",
3107+
"image/gif",
3108+
"text/latex",
3109+
"text/vnd.plotly.v1+html"
3110+
]
3111+
}
3112+
],
3113+
"notebookProvider": [
3114+
{
3115+
"viewType": "jupyter-notebook",
3116+
"displayName": "Jupyter Notebook (preview)",
3117+
"selector": [
3118+
{
3119+
"filenamePattern": "*.ipynb"
3120+
}
3121+
],
3122+
"priority": "option"
3123+
}
3124+
]
30893125
},
30903126
"scripts": {
30913127
"package": "gulp clean && gulp prePublishBundle && vsce package -o ms-python-insiders.vsix",

src/client/datascience/notebook/integration.ts

Lines changed: 22 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
// Licensed under the MIT License.
33
'use strict';
44
import { inject, injectable } from 'inversify';
5-
import * as path from 'path';
5+
import { ConfigurationTarget } from 'vscode';
66
import { IExtensionSingleActivationService } from '../../activation/types';
77
import {
88
IApplicationEnvironment,
99
IApplicationShell,
10-
ICommandManager,
11-
IVSCodeNotebook
10+
IVSCodeNotebook,
11+
IWorkspaceService
1212
} from '../../common/application/types';
1313
import { NotebookEditorSupport } from '../../common/experiments/groups';
1414
import { traceError } from '../../common/logger';
15-
import { IFileSystem } from '../../common/platform/types';
16-
import { IDisposableRegistry, IExperimentsManager, IExtensionContext } from '../../common/types';
15+
import { IDisposableRegistry, IExperimentsManager } from '../../common/types';
1716
import { DataScience } from '../../common/utils/localize';
1817
import { noop } from '../../common/utils/misc';
1918
import { JupyterNotebookView } from './constants';
@@ -33,36 +32,18 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
3332
@inject(IExperimentsManager) private readonly experiment: IExperimentsManager,
3433
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
3534
@inject(INotebookContentProvider) private readonly notebookContentProvider: INotebookContentProvider,
36-
@inject(IExtensionContext) private readonly context: IExtensionContext,
37-
@inject(IFileSystem) private readonly fs: IFileSystem,
38-
@inject(ICommandManager) private readonly commandManager: ICommandManager,
3935
@inject(NotebookKernel) private readonly notebookKernel: NotebookKernel,
4036
@inject(NotebookOutputRenderer) private readonly renderer: NotebookOutputRenderer,
4137
@inject(IApplicationEnvironment) private readonly env: IApplicationEnvironment,
42-
@inject(IApplicationShell) private readonly shell: IApplicationShell
38+
@inject(IApplicationShell) private readonly shell: IApplicationShell,
39+
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService
4340
) {}
44-
public get isEnabled() {
45-
const packageJsonFile = path.join(this.context.extensionPath, 'package.json');
46-
const content = JSON.parse(this.fs.readFileSync(packageJsonFile));
47-
48-
// This code is temporary.
49-
return (
50-
content.enableProposedApi &&
51-
Array.isArray(content.contributes.notebookOutputRenderer) &&
52-
(content.contributes.notebookOutputRenderer as []).length > 0 &&
53-
Array.isArray(content.contributes.notebookProvider) &&
54-
(content.contributes.notebookProvider as []).length > 0
55-
);
56-
}
57-
public async enableSideBySideUsage() {
58-
await this.enableNotebooks(false);
59-
}
6041
public async activate(): Promise<void> {
6142
// This condition is temporary.
6243
// If user belongs to the experiment, then make the necessary changes to package.json.
6344
// Once the API is final, we won't need to modify the package.json.
6445
if (this.experiment.inExperiment(NotebookEditorSupport.nativeNotebookExperiment)) {
65-
await this.enableNotebooks(true);
46+
await this.enableNotebooks();
6647
}
6748
if (this.env.channel !== 'insiders') {
6849
return;
@@ -108,62 +89,29 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
10889
}
10990
}
11091
}
111-
private async enableNotebooks(useVSCodeNotebookAsDefaultEditor: boolean) {
92+
private async enableNotebooks() {
11293
if (this.env.channel === 'stable') {
11394
this.shell.showErrorMessage(DataScience.previewNotebookOnlySupportedInVSCInsiders()).then(noop, noop);
11495
return;
11596
}
116-
const packageJsonFile = path.join(this.context.extensionPath, 'package.json');
117-
const content = JSON.parse(this.fs.readFileSync(packageJsonFile));
11897

11998
// This code is temporary.
99+
const settings = this.workspace.getConfiguration('workbench', undefined);
100+
const editorAssociations = settings.get('editorAssociations') as {
101+
viewType: string;
102+
filenamePattern: string;
103+
}[];
104+
120105
if (
121-
!content.enableProposedApi ||
122-
!Array.isArray(content.contributes.notebookOutputRenderer) ||
123-
!Array.isArray(content.contributes.notebookProvider)
106+
!Array.isArray(editorAssociations) ||
107+
editorAssociations.length === 0 ||
108+
!editorAssociations.find((item) => item.viewType === JupyterNotebookView)
124109
) {
125-
content.enableProposedApi = true;
126-
content.contributes.notebookOutputRenderer = [
127-
{
128-
viewType: 'jupyter-notebook-renderer',
129-
displayName: 'Jupyter Notebook Renderer',
130-
mimeTypes: [
131-
'application/geo+json',
132-
'application/vdom.v1+json',
133-
'application/vnd.dataresource+json',
134-
'application/vnd.plotly.v1+json',
135-
'application/vnd.vega.v2+json',
136-
'application/vnd.vega.v3+json',
137-
'application/vnd.vega.v4+json',
138-
'application/vnd.vega.v5+json',
139-
'application/vnd.vegalite.v1+json',
140-
'application/vnd.vegalite.v2+json',
141-
'application/vnd.vegalite.v3+json',
142-
'application/vnd.vegalite.v4+json',
143-
'application/x-nteract-model-debug+json',
144-
'image/gif',
145-
'text/latex',
146-
'text/vnd.plotly.v1+html'
147-
]
148-
}
149-
];
150-
content.contributes.notebookProvider = [
151-
{
152-
viewType: JupyterNotebookView,
153-
displayName: 'Jupyter Notebook (preview)',
154-
selector: [
155-
{
156-
filenamePattern: '*.ipynb'
157-
}
158-
],
159-
priority: useVSCodeNotebookAsDefaultEditor ? 'default' : 'option'
160-
}
161-
];
162-
163-
await this.fs.writeFile(packageJsonFile, JSON.stringify(content, undefined, 4));
164-
await this.commandManager
165-
.executeCommand('python.reloadVSCode', DataScience.reloadVSCodeNotebookEditor())
166-
.then(noop, noop);
110+
editorAssociations.push({
111+
viewType: 'jupyter-notebook',
112+
filenamePattern: '*.ipynb'
113+
});
114+
await settings.update('editorAssociations', editorAssociations, ConfigurationTarget.Global);
167115
}
168116
}
169117
}

src/client/datascience/notebook/notebookEditorProvider.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { JupyterNotebookView } from './constants';
2828
import { mapVSCNotebookCellsToNotebookCellModels } from './helpers/cellMappers';
2929
import { updateCellModelWithChangesToVSCCell } from './helpers/cellUpdateHelpers';
3030
import { isJupyterNotebook } from './helpers/helpers';
31-
import { NotebookIntegration } from './integration';
3231
import { NotebookEditor } from './notebookEditor';
3332
import { INotebookContentProvider, INotebookExecutionService } from './types';
3433

@@ -85,13 +84,6 @@ export class NotebookEditorProvider implements INotebookEditorProvider {
8584
if (uri) {
8685
setSharedProperty('ds_notebookeditor', 'native');
8786
captureTelemetry(Telemetry.OpenNotebook, { scope: 'command' }, false);
88-
const integration = serviceContainer.get<NotebookIntegration>(NotebookIntegration);
89-
// If user is not meant to be using VSC Notebooks, and it is not enabled,
90-
// then enable it for side by side usage.
91-
if (!integration.isEnabled && !useVSCodeNotebookEditorApi) {
92-
// At this point we need to reload VS Code, hence return and do not try to load nb, else it will fail.
93-
return integration.enableSideBySideUsage();
94-
}
9587
this.open(uri).ignoreErrors();
9688
}
9789
})

0 commit comments

Comments
 (0)