Skip to content

Instantly share code, notes, and snippets.

@blink1073
Last active June 2, 2016 19:12
Show Gist options
  • Save blink1073/ac460d8cc9911d77d82b6d7862f06c55 to your computer and use it in GitHub Desktop.
Save blink1073/ac460d8cc9911d77d82b6d7862f06c55 to your computer and use it in GitHub Desktop.
Create a session for a notebook
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;
}
@blink1073
Copy link
Author

Use as:

fetchNotebook('foo').then(content => {
  createSession('foo', content).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);
  });
});

@fpliger
Copy link

fpliger commented May 24, 2016

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);
  });
});

@fpliger
Copy link

fpliger commented May 24, 2016

FYI, there's a typo in https://gist.github.com/blink1073/ac460d8cc9911d77d82b6d7862f06c55#file-createsession-ts-L24

content.metdata -> content.metadata

:)

@blink1073
Copy link
Author

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