{"id":57659,"date":"2024-01-18T18:51:47","date_gmt":"2024-01-18T18:51:47","guid":{"rendered":"https:\/\/www.askpython.com\/?p=57659"},"modified":"2025-04-10T20:47:01","modified_gmt":"2025-04-10T20:47:01","slug":"integrating-python-functions-into-javascript","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/integrating-python-functions-into-javascript","title":{"rendered":"Python-JavaScript Integration: Guide to WebAssembly and Node.js"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Calling Python functions from JavaScript allows you to bring the power of Python to your web application. Let&#8217;s see how to call Python function from JavaScript code.<\/p>\n\n\n\n<p><em><strong>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/call-java-using-python\" data-type=\"post\" data-id=\"54904\">How to Call Java using Python with JPype and Pyjnius<\/a><\/strong><\/em><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Integrating Python functions into JavaScript enriches web applications with Python&#8217;s robust capabilities. Methods like WebAssembly and Node.js &#8216;child_process&#8217; module facilitate this integration. WebAssembly involves creating a module from Python code, while &#8216;child_process&#8217; executes Python scripts within JavaScript, both methods enabling seamless use of Python functionalities in a JavaScript environment<\/em><\/p>\n<\/blockquote>\n\n\n\n<p class=\"has-text-align-left\">There are some methods to call a Python function from JavaScript. The first method is we need to use a technology called &#8220;WebAssembly&#8221;.  WebAssembly is used to run in web browsers. Another method is using the Node.js Child Process Module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">WebAssembly for Python &#8211; JavaScript Integration<\/h3>\n\n\n\n<p>To call a Python function from JavaScript using <strong>WebAssembly<\/strong> you need to follow some steps: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef add(a, b):\n   return a + b\n<\/pre><\/div>\n\n\n<p>This is a simple function of the addition of two numbers. The output of this function will be the addition.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npythonjs -O3 -j add.py -o add.js\n<\/pre><\/div>\n\n\n<p>This will compile the Python function to JavaScript and WebAssembly code and save it with a file add.js.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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 <strong>module.exports<\/strong> object. This object contains all the functions and variables defined in the WebAssembly module. Here is an example of how to call <strong>add()<\/strong> function:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nfetch(&#039;add.js&#039;)\n   .then(response =&gt; response.arrayBuffer())\n   .then(buffer =&gt; WebAssembly.instantiate(buffer))\n   .then(module =&gt; {\n      const add = module.exports.add;\n      const result = add (1, 2);\n      console.log(result);\n    });\n<\/pre><\/div>\n\n\n<p>In this example, we first load <strong>add.js<\/strong> module using function<strong> fetch().<\/strong> Then we use the <strong>WebAssembly.instantiate()<\/strong> function to create WebAssembly module. Now we can access the add() function using <strong>module.exports<\/strong> object. Then finally we call the <strong>add()<\/strong> function with the arguments 1 and 2 and store the result in the variable called the <strong>result<\/strong>. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now run your JavaScript code in any JavaScript environment that supports WebAssembly. <\/li>\n<\/ul>\n\n\n\n<p>And the output is 3.<\/p>\n\n\n\n<p><strong> <em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" data-type=\"post\" data-id=\"285\">Python Functions<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using Node.js &#8216;child_process&#8217; for Python Integration<\/h3>\n\n\n\n<p>To call a Python function from JavaScript using the &#8216;child_process&#8217; Module you need to follow some steps: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, you have to create a Python script named example.py<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef my_function():\n    return &quot;Hello World!&quot;\n<\/pre><\/div>\n\n\n<p>output: <strong>Hello World!<\/strong><\/p>\n\n\n\n<p>This is a Python script with <strong>my_function()<\/strong> function. It returns the output as<strong> Hello World!.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Then you have to create a JavaScript script with named example.js.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst { execSync } = require(&#039;child_process&#039;);\n\nconst output = execSync(&#039;python -c &quot;from add import my_function; print(my_function())&quot;&#039;).toString();\n\nconsole.log(output);\n\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"146\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/JSCode-1024x146.png\" alt=\"JSCode\" class=\"wp-image-57921\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/JSCode-1024x146.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/JSCode-300x43.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/JSCode-768x110.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/JSCode.png 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This JavaScript code uses <strong>&#8216;execSync<\/strong>&#8216; function from Node.js &#8216;child_process&#8217; module. <strong>exeSync <\/strong>function used to run Python script named add.py. It captures the output of the command. <strong>toString()<\/strong> is called to output to convert Buffer object to a string. <strong>console.log()<\/strong> prints the captured output from the Python script.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now run your JavaScript code.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"223\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Example_output-1024x223.png\" alt=\"Example Output\" class=\"wp-image-57924\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Example_output-1024x223.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Example_output-300x65.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Example_output-768x167.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Example_output-1536x334.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Example_output.png 1577w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>output<\/strong>: Hello World!<\/p>\n\n\n\n<p>The output of example.js is <strong>Hello World!<\/strong> <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1703685955694\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why would I want to call a Python function using JavaScript code?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>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. <\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1703686555472\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are practical use cases for calling Python functions from JavaScript?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>1. Web Application<br \/>2. Data Processing<br \/>3. Machine Learning.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1703686667318\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How can I call a Python function from JavaScript?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>There are various methods, such as WebAssembly, Node.js child processes, or libraries like &#8216;python-shell&#8217;.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Here we use two methods to call a Python function from JavaScript code with example.<\/p>\n\n\n\n<p>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 <strong>module.exports <\/strong>object. In the second method<strong>,<\/strong> we use<strong> the child_process<\/strong> module.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">References:<\/h4>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/13175510\/call-python-function-from-javascript-code\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/13175510\/call-python-function-from-javascript-code<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calling Python functions from JavaScript allows you to bring the power of Python to your web application. Let&#8217;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&#8217;s robust capabilities. Methods like WebAssembly and [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":64089,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-57659","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=57659"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57659\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64089"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=57659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=57659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=57659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}