1

What I am currently able to do

const { spawn } = require("child_process");

exports.fetchWeatherdata = (location) => {
  console.log("DIR NAME DB" + __dirname)
  return new Promise((resolve, reject) => {
    let buf = "";
    const python = spawn("python", [
      __dirname + "/weathergetter.py",
      location.toString(),
    ]);

    python.stdout.on("data", (data) => {
      buf += data;
    });

    python.stderr.on("data", (data) => {
      console.error(`stderr: ${data}`);
    });

    python.on("close", (code) => {
      if (code !== 0) {
        return reject(`child process died with ${code}`);
      }
      const dataToSend = JSON.parse(buf.toString().replace(/\'/g, '"'));
      return resolve(dataToSend);
    });
  });
};


//in another file
const { fetchWeatherdata } = require('../python/weather')
exports.sendData = (req, res) => {
    console.log(req.query)
    console.log(req.params)

    async function main() {
        var wea = await fetchWeatherdata(req.params.loc);
        // console.log(wea);
        res.send(wea)
    }
    main()
}



What I want to achieve

const { spawn } = require("child_process");

exports.pythonFileRunner = (pathToFile, arguments) => {
   // some code goes here. This is where I need help
   return "output of the python file 📂"
}

//in another file
const { pythonFileRunner } = require('../python/weather')

exports.fetchWeatherdata = (location) => {
   //something like this ↓↓↓
   data = pythonFileRunner("path/to/file/main.py", location)
   return data
}

Basically, I want to create a function that can run any given python file with or without arguments and return its output.
Please Note: I want to finish all the async-await stuff inside the pythonFileRunner() function. This function must return only the output, which I can modify according to my usecase

If I am taking the wrong approach, please let me know in the comments.

2 Answers 2

2

It should be basically the same. Just replace

    const python = spawn("python", [
      __dirname + "/weathergetter.py",
      location.toString(),
    ]);

with

    const python = spawn("python", [
      pathToFile,
      ...arguments
    ]);
Sign up to request clarification or add additional context in comments.

10 Comments

I want to finish all the async await stuff inside the pythonFileRunner() function. This function must return only the output, which I can modify according to my usecase
If you copy the rest of the code from fetchWeatherData() it will do that.
Can you please show how
It's whatever is equivalent to the $PATH environment variable, maybe %PATH%? And start the script with the Windows equivalent of #!/usr/bin/python.
Can you help with this Javascriptcript question question
|
1

I have modified the fetchweatherdata function little to get what I wanted.

const { spawn } = require("child_process");

function pythonRunner(path, arguments) {
    return new Promise((resolve, reject) => {
        let buf = "";
        arguments.unshift(path)
        const python = spawn("python", arguments);
    
        python.stdout.on("data", (data) => {
          buf += data;
        });
    
        python.stderr.on("data", (data) => {
          console.error(`stderr: ${data}`);
        });
    
        python.on("close", (code) => {
          if (code !== 0) {
            return reject(`child process died with ${code}`);
          }
          const dataToSend = buf
          return resolve(dataToSend);
        });
      });
}



//in any other file
//first import the function

//how to use the function
(async () => {
  data = await pythonRunner("path/to/python/file", ["arg1", "arg2"])
  console.log(data)
})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.