Jupyter without Jupyter: Running Python Scripts in my JavaScript Application

If you want to do data processing as a JavaScript engineer, you’ll quickly learn the mathematical limitations of JavaScript. For data, we turn to Python, a long favorite of many scientists.

As a JavaScript engineer, I’ve learned to love the easy of web development with JavaScript. Having recently discovered Electron, it’s opening up new worlds in the world of JS for me. From online to offline, JS seems to be key to dominating app creations of the future.

So, how do you get these two major technologies to work together? How can I quick prototype an offline app with JavaScript and have it run Python codes?

A quick search on the Internet will lead most people to Jupyter Notebook. Jupyter Notebook was once called iPython Notebook, and from that second name we can easily decipher the core of what Jupyter does. In a snippet, Jupyter Notebook uses the iPython kernel (and other kernels for other languages) to create a coding environment where users can input code and see immediate results of their code output. It’s kind of like a Python playground on steroids.

However, I wanted something that’s even more customizable than Jupyter Notebook, and as something I can integrate into my current application as a JavaScript component. Basically, I wanted to create a Jupyter Notebook without the Jupyter.

Here’s a brief walk-through of how I went about it.

Since I am running on Electron, I needed to first embed an express server inside of Electron’s main process:

var server = require(‘./app’);

Then I create the app.js in the directory where I put all my usual express server codes wrapped in an iife:

(function() { 
var express = require('express');
var app = express();

The key to linking your Python script with your JavaScript application is the magic that is python-shell, an npm package that can be easily required into your express server and run your Python scripts from your JavaScript applications.

After you npm install python-shell, you need to require it into your app.js file:

var PythonShell = require('python-shell');

After the package is required, you can now send information to your Python script, run your Python script and get returned values from your Python script.

To with with any information to/from your Python script, first you want to set up the path to your .py file:

var pyshell = new PythonShell('py/main.py', { scriptPath: publicPath } );

In my case, I called it main. The option (after py/main.py) here includes scriptPath, which I set as my publicPath, leading to the folder where I am keeping all of my application related files. Then you can send and receive information from your Python files using the .send and .on methods:

pyshell.on('message', function (message) {
pyshell.end(function (err) {
if (err) throw err;

Just remember to end your process and to use ‘print’ in your Python script to return the values.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.