-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Select kernel based on metadata in notebook #14217
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Use the kernel defined in the metadata of Notebook instead of using the default workspace interpreter. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ import { IInterpreterLocatorService, IInterpreterService, KNOWN_PATH_SERVICE } f | |
import { captureTelemetry } from '../../telemetry'; | ||
import { getRealPath } from '../common'; | ||
import { Telemetry } from '../constants'; | ||
import { defaultKernelSpecName } from '../jupyter/kernels/helpers'; | ||
import { JupyterKernelSpec } from '../jupyter/kernels/jupyterKernelSpec'; | ||
import { IDataScienceFileSystem, IJupyterKernelSpec } from '../types'; | ||
import { IKernelFinder } from './types'; | ||
|
@@ -70,35 +69,32 @@ export class KernelFinder implements IKernelFinder { | |
const kernelName = kernelSpecMetadata?.name; | ||
|
||
if (kernelSpecMetadata && kernelName) { | ||
// For a non default kernelspec search for it | ||
if (!kernelName.includes(defaultKernelSpecName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not required, its not possible for us to get to this state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it not possible for the kernel spec to not start off as the default one? What if it comes from an external notebook? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method gets executed only in DefaultKernelSpec is something we generate, and that happens well after searching for a kernel. |
||
let kernelSpec = await this.searchCache(kernelName); | ||
let kernelSpec = await this.searchCache(kernelName); | ||
|
||
if (kernelSpec) { | ||
return kernelSpec; | ||
} | ||
if (kernelSpec) { | ||
return kernelSpec; | ||
} | ||
|
||
// Check in active interpreter first | ||
kernelSpec = await this.getKernelSpecFromActiveInterpreter(kernelName, resource); | ||
// Check in active interpreter first | ||
kernelSpec = await this.getKernelSpecFromActiveInterpreter(kernelName, resource); | ||
|
||
if (kernelSpec) { | ||
this.writeCache().ignoreErrors(); | ||
return kernelSpec; | ||
} | ||
|
||
const diskSearch = this.findDiskPath(kernelName); | ||
const interpreterSearch = this.getInterpreterPaths(resource).then((interpreterPaths) => { | ||
return this.findInterpreterPath(interpreterPaths, kernelName); | ||
}); | ||
if (kernelSpec) { | ||
this.writeCache().ignoreErrors(); | ||
return kernelSpec; | ||
} | ||
|
||
let result = await Promise.race([diskSearch, interpreterSearch]); | ||
if (!result) { | ||
const both = await Promise.all([diskSearch, interpreterSearch]); | ||
result = both[0] ? both[0] : both[1]; | ||
} | ||
const diskSearch = this.findDiskPath(kernelName); | ||
const interpreterSearch = this.getInterpreterPaths(resource).then((interpreterPaths) => { | ||
return this.findInterpreterPath(interpreterPaths, kernelName); | ||
}); | ||
|
||
foundKernel = result; | ||
let result = await Promise.race([diskSearch, interpreterSearch]); | ||
if (!result) { | ||
const both = await Promise.all([diskSearch, interpreterSearch]); | ||
result = both[0] ? both[0] : both[1]; | ||
} | ||
|
||
foundKernel = result; | ||
} | ||
|
||
this.writeCache().ignoreErrors(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,22 @@ export function updateNotebookMetadata( | |
kernelConnection && kernelConnectionMetadataHasKernelModel(kernelConnection) | ||
? kernelConnection.kernelModel | ||
: kernelConnection?.kernelSpec; | ||
if (kernelSpecOrModel && !metadata.kernelspec) { | ||
if (kernelConnection?.kind === 'startUsingPythonInterpreter') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the fix. |
||
// Store interpreter name, we expect the kernel finder will find the corresponding interpreter based on this name. | ||
const name = kernelConnection.interpreter.displayName || ''; | ||
if (metadata.kernelspec?.name !== name || metadata.kernelspec?.display_name !== name) { | ||
changed = true; | ||
metadata.kernelspec = { | ||
name, | ||
display_name: name, | ||
metadata: { | ||
interpreter: { | ||
hash: sha256().update(kernelConnection.interpreter.path).digest('hex') | ||
} | ||
} | ||
}; | ||
} | ||
} else if (kernelSpecOrModel && !metadata.kernelspec) { | ||
// Add a new spec in this case | ||
metadata.kernelspec = { | ||
name: kernelSpecOrModel.name || kernelSpecOrModel.display_name || '', | ||
|
@@ -91,20 +106,12 @@ export function updateNotebookMetadata( | |
metadata.kernelspec.display_name = displayName; | ||
kernelId = kernelSpecOrModel.id; | ||
} | ||
} else if (kernelConnection?.kind === 'startUsingPythonInterpreter') { | ||
// Store interpreter name, we expect the kernel finder will find the corresponding interpreter based on this name. | ||
const name = kernelConnection.interpreter.displayName || ''; | ||
if (metadata.kernelspec?.name !== name || metadata.kernelspec?.display_name !== name) { | ||
changed = true; | ||
metadata.kernelspec = { | ||
name, | ||
display_name: name, | ||
metadata: { | ||
interpreter: { | ||
hash: sha256().update(kernelConnection.interpreter.path).digest('hex') | ||
} | ||
} | ||
}; | ||
try { | ||
// This is set only for when we select an interpreter. | ||
// tslint:disable-next-line: no-any | ||
delete (metadata.kernelspec as any).metadata; | ||
} catch { | ||
// Noop. | ||
} | ||
} | ||
return { changed, kernelId }; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this, not required.