Last active
June 2, 2016 19:12
-
-
Save blink1073/ac460d8cc9911d77d82b6d7862f06c55 to your computer and use it in GitHub Desktop.
Create a session for a notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
INotebookSession, IKernelSpecIds, ContentsManager, startNewSession, | |
getKernelSpecs | |
} from 'jupyter-js-services'; | |
/** | |
* Fetch notebook contents by path. | |
*/ | |
export | |
function fetchNotebook(path: string, baseUrl?: string, ajaxSettings?: string): Promise<any> { | |
let contentsManager = new ContentsManager(baseUrl, ajaxSettings); | |
return contentsManager.get(path, { type: 'notebook'} ).then(contents => { | |
return contents.content; | |
}); | |
} | |
/** | |
* Create an appropriate notebook session for a given path. | |
*/ | |
export | |
function createSession(notebookPath: string, content: any, baseUrl?: string, ajaxSettings?: string): Promise<INotebookSession> { | |
let metadata = content.metadata; | |
let name = metadata.kernelspecs ? metadata.kernelspecs.name : 'unknown'; | |
let lang = metadata.language_info ? metadata.language_info.name : 'unknown'; | |
return getKernelSpecs({ baseUrl, ajaxSettings }).then(specs => { | |
let kernelName = findKernel(name, lang, specs); | |
return startNewSession({ kernelName, notebookPath, baseUrl, ajaxSettings }); | |
}); | |
} | |
/** | |
* Get the appropriate kernel name given a notebook model. | |
*/ | |
function findKernel(kernelName: string, language: string, specs: IKernelSpecIds): string { | |
if (kernelName === 'unknown') { | |
return specs.default; | |
} | |
// Look for an exact match. | |
for (let specName in specs.kernelspecs) { | |
if (specName === kernelName) { | |
return kernelName; | |
} | |
} | |
// Next try to match the language name. | |
if (language === 'unknown') { | |
return specs.default; | |
} | |
for (let specName in specs.kernelspecs) { | |
let kernelLanguage = specs.kernelspecs[name].spec.language; | |
if (language === kernelLanguage) { | |
console.log('No exact match found for ' + name + | |
', using kernel ' + specName + ' that matches ' + | |
'language=' + language); | |
return specName; | |
} | |
} | |
// Finally, use the default kernel. | |
console.log(`No matching kernel found for ${kernelName}, ` + | |
`using default kernel ${specs.default}`); | |
return specs.default; | |
} |
Use as: ...
Shouldn't fetchNotebook
be called with at least path
and baseUrl
? So should be something like:
fetchNotebook('foo.ipynb', 'localhost:8888').then(content => {
createSession('foo.ipynb', content, 'localhost:8888').then(session => {
let code = content.cells[0].source;
let request = {
code,
silent: false,
store_history: true,
stop_on_error: true,
allow_stdin: true
};
session.kernel.execute(request);
});
});
FYI, there's a typo in https://gist.github.com/blink1073/ac460d8cc9911d77d82b6d7862f06c55#file-createsession-ts-L24
content.metdata
-> content.metadata
:)
If you don't provide a baseUrl, it will try and find it through the standard means.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use as: