Skip to content
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

Kernel selection UI #8866

Merged
merged 22 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 24 additions & 2 deletions src/client/datascience/interactive-common/interactiveBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
import { JupyterInstallError } from '../jupyter/jupyterInstallError';
import { JupyterSelfCertsError } from '../jupyter/jupyterSelfCertsError';
import { JupyterKernelPromiseFailedError } from '../jupyter/kernels/jupyterKernelPromiseFailedError';
import { KernelSpecInterpreter } from '../jupyter/kernels/kernelSelector';
import { CssMessages } from '../messages';
import {
CellState,
Expand Down Expand Up @@ -1303,14 +1304,35 @@ export abstract class InteractiveBase extends WebViewHost<IInteractiveWindowMapp
private async selectKernel() {
const settings = this.configuration.getSettings();

let kernel: KernelSpecInterpreter | undefined;

if (settings.datascience.jupyterServerURI.toLowerCase() === Settings.JupyterServerLocalLaunch) {
return this.dataScience.selectLocalJupyterKernel();
kernel = await this.dataScience.selectLocalJupyterKernel();
} else if (this.notebook) {
const connInfo = this.notebook.server.getConnectionInfo();

if (connInfo) {
await this.dataScience.selectRemoteJupyterKernel(connInfo);
kernel = await this.dataScience.selectRemoteJupyterKernel(connInfo);
}
}

if (kernel && kernel.kernelSpec && this.notebook) {
let name = kernel.kernelSpec?.display_name;

Choose a reason for hiding this comment

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

kernel.kernelSpec? isn't necessary, as the if condition has ensured kernelSpec isn't undefined.

if (!name) {
name = kernel.interpreter?.displayName ? kernel.interpreter.displayName : '';
}
this.postMessage(InteractiveWindowMessages.UpdateKernel, {
jupyterServerStatus: ServerStatus.Starting,
localizedUri: settings.datascience.jupyterServerURI.toLowerCase() === Settings.JupyterServerLocalLaunch ?
localize.DataScience.localJupyterServer() : settings.datascience.jupyterServerURI,
displayName: name
}).ignoreErrors();

// Also actually tell the kernel.
await this.notebook.setKernelSpec(kernel.kernelSpec);

Choose a reason for hiding this comment

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

Should't this be done first, before we update the UI?
What if this fails.

Copy link

Choose a reason for hiding this comment

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

Yeah that's probably better. Let me try that. I think I can eliminate the status update then too.


In reply to: 356777545 [](ancestors = 356777545)


// Add in a new sys info
await this.addSysInfo(SysInfoReason.New);
}
}
}
17 changes: 17 additions & 0 deletions src/client/datascience/jupyter/jupyterNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,23 @@ export class JupyterNotebookBase implements INotebook {
return this.launchInfo.kernelSpec;
}

public async setKernelSpec(spec: IJupyterKernelSpec): Promise<void> {
// Change our own kernel spec
this.launchInfo.kernelSpec = spec;

// We need to start a new session with the new kernel spec
if (this.session) {
// Turn off setup
this.ranInitialSetup = false;

// Change the kernel on the session
await this.session.changeKernel(spec);

// Rerun our initial setup
await this.initialize();
}
}

private finishUncompletedCells() {
const copyPending = [...this.pendingCellSubscriptions];
copyPending.forEach(c => c.cancel());
Expand Down
20 changes: 20 additions & 0 deletions src/client/datascience/jupyter/jupyterSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ export class JupyterSession implements IJupyterSession {
return this.connected;
}

public async changeKernel(kernel: IJupyterKernelSpec): Promise<void> {
// This is just like doing a restart, kill the old session (and the old restart session), and start new ones
if (this.session?.kernel) {
this.shutdownSession(this.session, this.statusHandler).ignoreErrors();
this.restartSessionPromise?.then(r => this.shutdownSession(r, undefined)).ignoreErrors();
}

// Update our kernel spec
this.kernelSpec = kernel;

// Start a new session
this.session = await this.createSession(this.serverSettings, this.contentsManager);

// Listen for session status changes
this.session.statusChanged.connect(this.statusHandler);

// Start the restart session promise too.
this.restartSessionPromise = this.createRestartSession(this.serverSettings, this.contentsManager);
}

private getServerStatus(): ServerStatus {
if (this.session) {
switch (this.session.kernel.status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ export class GuestJupyterNotebook
return;
}

public setKernelSpec(_spec: IJupyterKernelSpec): Promise<void> {
return Promise.resolve();
}

private onServerResponse = (args: Object) => {
const er = args as IExecuteObservableResponse;
traceInfo(`Guest serverResponse ${er.pos} ${er.id}`);
Expand Down
2 changes: 2 additions & 0 deletions src/client/datascience/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface INotebook extends IAsyncDisposable {
addLogger(logger: INotebookExecutionLogger): void;
getMatchingInterpreter(): PythonInterpreter | undefined;
getKernelSpec(): IJupyterKernelSpec | undefined;
setKernelSpec(spec: IJupyterKernelSpec): Promise<void>;
}

export interface INotebookServerOptions {
Expand Down Expand Up @@ -171,6 +172,7 @@ export interface IJupyterSession extends IAsyncDisposable {
requestExecute(content: KernelMessage.IExecuteRequestMsg['content'], disposeOnDone?: boolean, metadata?: JSONObject): Kernel.IShellFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg> | undefined;
requestComplete(content: KernelMessage.ICompleteRequestMsg['content']): Promise<KernelMessage.ICompleteReplyMsg | undefined>;
sendInputReply(content: string): void;
changeKernel(kernel: IJupyterKernelSpec): Promise<void>;
}

export const IJupyterSessionManagerFactory = Symbol('IJupyterSessionManagerFactory');
Expand Down
7 changes: 2 additions & 5 deletions src/datascience-ui/interactive-common/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ body, html {
padding: 2px 5px 2px 5px;
}

.kernel-status-section:hover {
.kernel-status-section-hoverable:hover {
background-color: var(--override-widget-background, var(--vscode-notifications-background));
cursor:pointer;
}

.kernel-status-divider {
Expand All @@ -474,7 +475,3 @@ body, html {
width: 16px;
height: 0px;
}

.kernel-status:hover {
cursor: pointer;
}
4 changes: 2 additions & 2 deletions src/datascience-ui/interactive-common/kernelSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export class KernelSelection extends React.Component<IKernelSelectionProps> {

return (
<div className='kernel-status' style={dynamicFont}>
<div className='kernel-status-section' onClick={this.props.selectServer} role='button'>
<div className='kernel-status-section' role='button'>
<div className='kernel-status-text'>
{getLocString('DataScience.jupyterServer', 'Jupyter Server')}: {this.props.kernel.localizedUri}
</div>
<Image baseTheme={this.props.baseTheme} class='image-button-image kernel-status-icon' image={this.getIcon()} />
</div>
<div className='kernel-status-divider'/>
<div className='kernel-status-section' onClick={this.props.selectKernel} role='button'>
<div className='kernel-status-section kernel-status-section-hoverable' onClick={this.props.selectKernel} role='button'>
{this.props.kernel.displayName}: {this.props.kernel.jupyterServerStatus}
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/test/datascience/execution.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class MockJupyterNotebook implements INotebook {
public getKernelSpec(): IJupyterKernelSpec | undefined {
return;
}

public setKernelSpec(_spec: IJupyterKernelSpec): Promise<void> {
return Promise.resolve();
}

public get onSessionStatusChanged(): Event<ServerStatus> {
if (!this.onStatusChangedEvent) {
this.onStatusChangedEvent = new EventEmitter<ServerStatus>();
Expand Down
20 changes: 19 additions & 1 deletion src/test/datascience/mockJupyterSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Kernel, KernelMessage } from '@jupyterlab/services';
import { JSONObject } from '@phosphor/coreutils/lib/json';
import { CancellationTokenSource, Event, EventEmitter } from 'vscode';
import { JupyterKernelPromiseFailedError } from '../../client/datascience/jupyter/kernels/jupyterKernelPromiseFailedError';
import { ICell, IJupyterSession } from '../../client/datascience/types';
import { ICell, IJupyterSession, IJupyterKernelSpec } from '../../client/datascience/types';
import { ServerStatus } from '../../datascience-ui/interactive-common/mainState';
import { sleep } from '../core';
import { MockJupyterRequest } from './mockJupyterRequest';
Expand All @@ -25,9 +25,18 @@ export class MockJupyterSession implements IJupyterSession {
private completionTimeout: number = 1;
private lastRequest: MockJupyterRequest | undefined;

private kernel: IJupyterKernelSpec;

constructor(cellDictionary: Record<string, ICell>, timedelay: number) {
this.dict = cellDictionary;
this.timedelay = timedelay;
this.kernel = {
name: 'First',
language: 'Python',
path: 'foo/bar/python',
display_name: 'First',
argv: []
};
}

public get onRestarted(): Event<void> {
Expand Down Expand Up @@ -133,6 +142,15 @@ export class MockJupyterSession implements IJupyterSession {
this.completionTimeout = timeout;
}

public changeKernel(kernel: IJupyterKernelSpec): Promise<void> {
this.kernel = kernel;
return Promise.resolve();
}

public getKernel(): IJupyterKernelSpec {
return this.kernel;
}

private findCell = (code: string): ICell => {
// Match skipping line separators
const withoutLines = code.replace(LineFeedRegEx, '').toLowerCase();
Expand Down