Nodebooks: Introducing Node.js Data Science Notebooks

Python and Node.js in the same Jupyter notebook (part 1)

Notebooks

Installing

!pip install pixiedust
!pip install pixiedust_node

Using pixiedust_node

import pixiedust_node
%%node
var date = new Date();
print(date);
// "2017-05-15T14:02:28.207Z"
The JS code and its output, as rendered in an IPython Notebook cell.

Printing JavaScript variables

%%node
var x = { a:1, b:'two', c: true };
print(x);
// {"a": 1, "b": "two", "c": true}

Using PixieDust display() to visualise data

%%node
var data = [];
for (var i = 0; i < 1000; i++) {
var x = 2*Math.PI * i/ 360;
var obj = {
x: x,
i: i,
sin: Math.sin(x),
cos: Math.cos(x),
tan: Math.tan(x)
};
data.push(obj);
}
display(data);
Using PixieDust’s display UI to refine a visualisation

Adding npm modules

npm.install('request');
%%node
var request = require('request');
var r = {
method:'GET',
url: 'http://api.open-notify.org/iss-now.json',
json: true
};
request(r, function(err, req, body) {
print(body);
});
// {"timestamp": 1494857069, "message": "success", "iss_position": {"latitude": "24.0980", "longitude": "-84.5517"}}
%%node
var request = require('request');
var getPosition = function(callback) {
var r = {
method:'GET',
url: 'http://api.open-notify.org/iss-now.json',
json: true
};
request(r, function(err, req, body) {
var obj = null;
if (!err) {
obj = body.iss_position
obj.latitude = parseFloat(obj.latitude);
obj.longitude = parseFloat(obj.longitude);
obj.time = new Date().getTime();
}
callback(err, obj);
});
};
%%node
getPosition(function(err, data) {
print(data);
});
// {"latitude": 50.5736, "longitude": -99.3493, "time": 1494422942373}

Promise me a miracle

npm.install( ('request', 'request-promise') )
%%node
var request = require('request-promise');
var getPosition = function(callback) {
var r = {
method:'GET',
url: 'http://api.open-notify.org/iss-now.json',
json: true
};
return request(r).then(function(body) {
var obj = null;
obj = body.iss_position
obj.latitude = parseFloat(obj.latitude);
obj.longitude = parseFloat(obj.longitude);
obj.time = new Date().getTime();
return obj;
});
};
%%node
getPosition().then(function(data) {
print(data);
});
// {"latitude": 20.7734, "longitude": -81.5809, "time": 1494857142842}
%%node
getPosition().then(print);
// {"latitude": 20.7734, "longitude": -81.5809, "time": 1494857142842}

Next time

Links

--

--

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store