Skip to content

Extension api for DataScience #13923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NotebookCell } from 'vscode-proposed';
import { isTestExecution } from './common/constants';
import { traceError } from './common/logger';
import { IConfigurationService, Resource } from './common/types';
import { JupyterExtensionIntegration } from './datascience/api/jupyterIntegration';
import { IDataViewerDataProvider, IDataViewerFactory } from './datascience/data-viewing/types';
import { IJupyterUriProvider, IJupyterUriProviderRegistration, INotebookExtensibility } from './datascience/types';
import { getDebugpyLauncherArgs, getDebugpyPackagePath } from './debugger/extension/adapter/remoteLaunchers';
Expand All @@ -26,6 +27,9 @@ export interface IExtensionApi {
* @memberof IExtensionApi
*/
ready: Promise<void>;
jupyter: {
registerHooks(): void;
};
debug: {
/**
* Generate an array of strings for commands to pass to the Python executable to launch the debugger for remote debugging.
Expand Down Expand Up @@ -103,12 +107,17 @@ export function buildApi(
const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const notebookExtensibility = serviceContainer.get<INotebookExtensibility>(INotebookExtensibility);
serviceManager.addSingleton<JupyterExtensionIntegration>(JupyterExtensionIntegration, JupyterExtensionIntegration);
const jupyterIntegration = serviceContainer.get<JupyterExtensionIntegration>(JupyterExtensionIntegration);
const api: IExtensionApi = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch((ex) => {
traceError('Failure during activation.', ex);
return Promise.reject(ex);
}),
jupyter: {
registerHooks: () => jupyterIntegration.integrateWithJupyterExtension()
},
debug: {
async getRemoteLauncherCommand(
host: string,
Expand Down
103 changes: 103 additions & 0 deletions src/client/datascience/api/jupyterIntegration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// tslint:disable-next-line: no-single-line-block-comment
/* eslint-disable comma-dangle */
// tslint:disable-next-line: no-single-line-block-comment
/* eslint-disable implicit-arrow-linebreak */
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { inject, injectable } from 'inversify';
import { CancellationToken, Event, Uri } from 'vscode';
import { InterpreterUri } from '../../common/installer/types';
import { IExtensions, IInstaller, InstallerResponse, Product, Resource } from '../../common/types';
import { IEnvironmentActivationService } from '../../interpreter/activation/types';
import { IInterpreterQuickPickItem, IInterpreterSelector } from '../../interpreter/configuration/types';
import { IInterpreterService } from '../../interpreter/contracts';
import { IWindowsStoreInterpreter } from '../../interpreter/locators/types';
import { WindowsStoreInterpreter } from '../../pythonEnvironments/discovery/locators/services/windowsStoreInterpreter';
import { PythonEnvironment } from '../../pythonEnvironments/info';

type PythonApiForJupyterExtension = {
/**
* IInterpreterService
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment indicates where we (DS) pulled these methods from.
Gives you an indication of our depdencies.

*/
onDidChangeInterpreter: Event<void>;
/**
* IInterpreterService
*/
getInterpreters(resource?: Uri): Promise<PythonEnvironment[]>;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We rely on PythonEnvironment, however we don't need all of the properties in this type.
Will update as we refactor the DS code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda looks like a rename might be nice here (Interpreter !== Environment) but for now seems better to stay matched up with how the core team has it.

/**
* IInterpreterService
*/
getActiveInterpreter(resource?: Uri): Promise<PythonEnvironment | undefined>;
/**
* IInterpreterService
*/
getInterpreterDetails(pythonPath: string, resource?: Uri): Promise<undefined | PythonEnvironment>;

/**
* IEnvironmentActivationService
*/
getActivatedEnvironmentVariables(
resource: Resource,
interpreter?: PythonEnvironment,
allowExceptions?: boolean
): Promise<NodeJS.ProcessEnv | undefined>;
isWindowsStoreInterpreter(pythonPath: string): Promise<boolean>;
/**
* IWindowsStoreInterpreter
*/
getSuggestions(resource: Resource): Promise<IInterpreterQuickPickItem[]>;
/**
* IInstaller
*/
install(product: Product, resource?: InterpreterUri, cancel?: CancellationToken): Promise<InstallerResponse>;
};

type JupyterExtensionApi = {
registerPythonApi(interpreterService: PythonApiForJupyterExtension): void;
};

@injectable()
export class JupyterExtensionIntegration {
constructor(
@inject(IExtensions) private readonly extensions: IExtensions,
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IInterpreterSelector) private readonly interpreterSelector: IInterpreterSelector,
@inject(WindowsStoreInterpreter) private readonly windowsStoreInterpreter: IWindowsStoreInterpreter,
@inject(IInstaller) private readonly installer: IInstaller,
@inject(IEnvironmentActivationService) private readonly envActivation: IEnvironmentActivationService
) {}

public async integrateWithJupyterExtension(): Promise<void> {
const jupyterExtension = this.extensions.getExtension<JupyterExtensionApi>('ms-ai-tools.jupyter');
if (!jupyterExtension) {
return;
}
await jupyterExtension.activate();
if (!jupyterExtension.isActive) {
return;
}
const jupyterExtensionApi = jupyterExtension.exports;
jupyterExtensionApi.registerPythonApi({
onDidChangeInterpreter: this.interpreterService.onDidChangeInterpreter,
getActiveInterpreter: async (resource?: Uri) => this.interpreterService.getActiveInterpreter(resource),
getInterpreterDetails: async (pythonPath: string) =>
this.interpreterService.getInterpreterDetails(pythonPath),
getInterpreters: async (resource: Uri | undefined) => this.interpreterService.getInterpreters(resource),
getActivatedEnvironmentVariables: async (
resource: Resource,
interpreter?: PythonEnvironment,
allowExceptions?: boolean
) => this.envActivation.getActivatedEnvironmentVariables(resource, interpreter, allowExceptions),
isWindowsStoreInterpreter: async (pythonPath: string): Promise<boolean> =>
this.windowsStoreInterpreter.isWindowsStoreInterpreter(pythonPath),
getSuggestions: async (resource: Resource): Promise<IInterpreterQuickPickItem[]> =>
this.interpreterSelector.getSuggestions(resource),
install: async (
product: Product,
resource?: InterpreterUri,
cancel?: CancellationToken
): Promise<InstallerResponse> => this.installer.install(product, resource, cancel)
});
}
}