-2

I have a lexer and parser for a custom language in python but due to some reason I need to use JS too. First I thought that I would build the two functions ( lexer and parser ) in JS again but it was possible only in Python.

Is there any way in which I could call the two Python functions in my JS code. I saw other questions on Stack Overflow but I want to be able to send arguments to the functions and get back data from those functions.

Please suggest any trick or method I can use.

Shardul Nalegave
  • 419
  • 9
  • 16
  • You can use Flask and set a Restful API to call those functions, take a look at this [guide](https://medium.com/python-pandemonium/build-simple-restful-api-with-python-and-flask-part-1-fae9ff66a706) – Gabriel Carneiro May 14 '18 at 17:21
  • Possible duplicate of [this](https://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code) – Muhammad Salman May 14 '18 at 17:21

1 Answers1

0

Node.js has a package called child_process and it has an exec function to execute any local script or program that you have. Here is a sort of boilerplate example:

const exec = require('child_process').exec;
var yourscript = exec('sh ~/some_bash.sh', (error, stdout, stderr) => {
        console.log('${stdout}');
        console.log('${stderr}');
        if (error !== null) {
            console.log('Uh oh! Error: ${error}');
        }
    });
Dylan Moore
  • 443
  • 4
  • 14
  • But how can I pass arguments to the functions and get back data. – Shardul Nalegave May 15 '18 at 04:00
  • You pass arguments as you would with flags in the terminal/command line and `stdout` is the universal file that the kernel sends output to. If you want to get back data, manage this variable. This function contains everything you need if you must execute python code from node or JS. If you are uncertain about these processes, it would be dutiful to read about them, "stdout" and "python script arguments" – Dylan Moore May 15 '18 at 12:52