Python-JavaScript Integration: Guide to WebAssembly and Node.js

Call Python From JavaScript Code

Calling Python functions from JavaScript allows you to bring the power of Python to your web application. Let’s see how to call Python function from JavaScript code.

Also read: How to Call Java using Python with JPype and Pyjnius

Integrating Python functions into JavaScript enriches web applications with Python’s robust capabilities. Methods like WebAssembly and Node.js ‘child_process’ module facilitate this integration. WebAssembly involves creating a module from Python code, while ‘child_process’ executes Python scripts within JavaScript, both methods enabling seamless use of Python functionalities in a JavaScript environment

There are some methods to call a Python function from JavaScript. The first method is we need to use a technology called “WebAssembly”. WebAssembly is used to run in web browsers. Another method is using the Node.js Child Process Module.

WebAssembly for Python – JavaScript Integration

To call a Python function from JavaScript using WebAssembly you need to follow some steps:

  • Write a Python function that you want to call from JavaScript. For example, a Python function that adds two numbers with a file named add.py:
def add(a, b):
   return a + b

This is a simple function of the addition of two numbers. The output of this function will be the addition.

  • Compile a Python function to WebAssembly using a tool Emscripten. Emscripten is a compiler that can compile Python code using the library called PythonJs. To compile, first you need to install Emscipten. Once you have installed Emscripten, you can compile the Python function to WebAssembly using the following command:
pythonjs -O3 -j add.py -o add.js

This will compile the Python function to JavaScript and WebAssembly code and save it with a file add.js.

  • Now call the Python function from the JavaScript using the WebAssembly module. To call a function in a WebAssembly module, you need to use the module.exports object. This object contains all the functions and variables defined in the WebAssembly module. Here is an example of how to call add() function:
fetch('add.js')
   .then(response => response.arrayBuffer())
   .then(buffer => WebAssembly.instantiate(buffer))
   .then(module => {
      const add = module.exports.add;
      const result = add (1, 2);
      console.log(result);
    });

In this example, we first load add.js module using function fetch(). Then we use the WebAssembly.instantiate() function to create WebAssembly module. Now we can access the add() function using module.exports object. Then finally we call the add() function with the arguments 1 and 2 and store the result in the variable called the result.

  • Now run your JavaScript code in any JavaScript environment that supports WebAssembly.

And the output is 3.

Also read: Python Functions


Using Node.js ‘child_process’ for Python Integration

To call a Python function from JavaScript using the ‘child_process’ Module you need to follow some steps:

  • First, you have to create a Python script named example.py
def my_function():
    return "Hello World!"

output: Hello World!

This is a Python script with my_function() function. It returns the output as Hello World!.

  • Then you have to create a JavaScript script with named example.js.
const { execSync } = require('child_process');

const output = execSync('python -c "from add import my_function; print(my_function())"').toString();

console.log(output);

JSCode

This JavaScript code uses ‘execSync‘ function from Node.js ‘child_process’ module. exeSync function used to run Python script named add.py. It captures the output of the command. toString() is called to output to convert Buffer object to a string. console.log() prints the captured output from the Python script.

  • Now run your JavaScript code.
Example Output

output: Hello World!

The output of example.js is Hello World!


Why would I want to call a Python function using JavaScript code?

Calling a Python function from JavaScript alllows you to utilize existing Python code or libraries within a JavaScript environment. This is beneficial when you want to integrate specific Python functionalities.

What are practical use cases for calling Python functions from JavaScript?

1. Web Application
2. Data Processing
3. Machine Learning.

How can I call a Python function from JavaScript?

There are various methods, such as WebAssembly, Node.js child processes, or libraries like ‘python-shell’.


Here we use two methods to call a Python function from JavaScript code with example.

These methods demonstrate how to call the Python function using JavaScript. The first method is using WebAssembly where we create a webAssembly module and use module.exports object. In the second method, we use the child_process module.


References:

https://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code