Skip to content

Commit

Permalink
Retrieve notebook metadata before making request to kernel gateway, s…
Browse files Browse the repository at this point in the history
…olves jupyter#169

(c) Copyright IBM Corp. 2016
  • Loading branch information
James Martin committed May 16, 2016
1 parent 4d02072 commit de29821
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion client/js/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var Services = require('jupyter-js-services');
var kernelOptions = {
baseUrl: kernelUrl,
wsUrl: kernelUrl.replace(/^http/, 'ws'),
name: 'python3',
name: '', // Set by API when making request to kernel gateway
clientId: clientId,
ajaxSettings: {
requestHeaders: {
Expand Down
75 changes: 37 additions & 38 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,52 +139,51 @@ router.post('/kernels', bodyParser.json({ type: 'text/plain' }), function(req, r
};
}

// Store the notebook path for use within the WS proxy.
var notebookPathHeader = req.headers['x-jupyter-notebook-path'];
var sessionId = req.headers['x-jupyter-session-id'];
if (!notebookPathHeader || !sessionId) {
error('Missing notebook path or session ID headers');
return res.status(500).end();
}

var matches = notebookPathHeader.match(/^\/(?:dashboards(-plain)?)?(.*)$/);
if (!matches) {
error('Invalid notebook path header');
return res.status(500).end();
}else{
var notebookPath = matches[2];
var kernel_lang;
var nb = nbstore.get(notebookPath)
.then(function success(notebook){
console.log("here");
kernel_lang = notebook.metadata.kernelspec.name;
console.log(kernel_lang);
});
}
console.log(kernel_lang);

// Pass the (modified) request to the kernel gateway.
request({
url: urljoin(kgUrl, kgBaseUrl, '/api/kernels'),
method: 'POST',
headers: headers,
json: req.body
}, function(err, response, body) {
if(err) {
error('Error proxying kernel creation request:' + err.toString());
return res.status(500).end();
}
// Store the notebook path for use within the WS proxy.
var notebookPathHeader = req.headers['x-jupyter-notebook-path'];
var sessionId = req.headers['x-jupyter-session-id'];
if (!notebookPathHeader || !sessionId) {
error('Missing notebook path or session ID headers');
return res.status(500).end();
}
var matches = notebookPathHeader.match(/^\/(?:dashboards(-plain)?)?(.*)$/);
if (!matches) {
error('Invalid notebook path header');
return res.status(500).end();
}
// Store notebook path for later use
sessions[sessionId] = matches[2];
sessions[sessionId] = notebookPath;

// Pass the kernel gateway response back to the client.
res.set(response.headers);
res.status(response.statusCode).json(body);
});
var kernel_lang;

nbstore.get(notebookPath)
.then(function success(notebook){
if (notebook.metadata.kernelspec.name){
req.body.name = notebook.metadata.kernelspec.name;
}else{
// Default to Python 3
req.body.name = 'python3';
}
// Pass the (modified) request to the kernel gateway.
request({
url: urljoin(kgUrl, kgBaseUrl, '/api/kernels'),
method: 'POST',
headers: headers,
json: req.body
}, function(err, response, body) {
if(err) {
error('Error proxying kernel creation request:' + err.toString());
return res.status(500).end();
}

// Pass the kernel gateway response back to the client.
res.set(response.headers);
res.status(response.statusCode).json(body);
});
});
}
});

// Proxy all unhandled requests to the kernel gateway.
Expand Down

0 comments on commit de29821

Please sign in to comment.