Sharing Variables Between Python & Node.js in Jupyter Notebooks

They live apart and speak different languages, but these variables hold common values

Glynn Bird
Jan 24, 2018 · 2 min read
!pip install pixiedust_node
import pixiedust_node
%%node
console.log("Today's date is", new Date());
npm.install( ('request', 'request-promise') )
%%node
const request = require('request-promise');
request.get('http://www.google.com').then(print);
Share Python and Node.js variables in multiple directions. Photo by Brendan Church on Unsplash

Sharing variables (shariables?)

With the release of pixiedust_node v0.2 you can now share data interchangeably between Python and Node.js cells. Declare some variables in Python:

a = 'hello'
b = 2
c = False
d = {'x':1, 'y':2}
e = 3.142
f = [{'a':1}, {'a':2}, {'a':3}]
%%node
console.log(a, b, c, d, e, f);
// hello 2 false {y:2, x:1} 3.142 [{ a:1 }, { a:2 }, { a:3 }]
%%node
var i = 'world';
var j = 3;
var k = true;
var l = { z:99 }
var m = 2.7182;
var n = ['apples', 'oranges'];
print(i,j,k,l,m,n)
# (u'world', 3, True, {u'z': 99}, 2.7182, [u'apples', u'oranges'])
%%node
var googlehomepage = '';
request.get('http://www.google.com').then(function(data) {
  googlehomepage = data;
  print('Fetched Google homepage');
});

How to upgrade

Simply run:

!pip --upgrade pixiedust_node

Why do this?

PixieDust already allows the sharing of data from Python cells to Scala cells. This feature is now extended to Node.js cells too.

IBM CODAIT

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

Glynn Bird

Written by

Developer @ IBM. https://glynnbird.com

IBM CODAIT

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