Tuesday, 18 April 2017

Node.js - Run python script on form submit

I have installed python-shell per the instructions on this site and am able to get this code to run on test.ejs on page load:

var PythonShell = require('python-shell');
var options = {
    scriptPath: '/nodeGit/scripts',
};

PythonShell.run('my_script.py', options, function (err) {
  if (err) throw err;
  console.log('finished');
});

I get this to run on test.ejs page load by putting it in test.js. The portion of test.js regarding my test.ejs page looks like this:

exports.test = function(req, res,callback){

   PythonShell.run('my_script.py', options, function (err, results) {
       if (err) throw err;
  // results is an array consisting of messages collected during execution 
  console.log('results: %j', results);
});

// The functions below were not written by me but were left in to show how functions are written(?) in hopes that it helps

    async.auto({

        getData: function get_data(callback){
            Images.findAll({where: {uploaded: 1}})
            .then(function(getData){
                    callback(null, getData);
                })
            .catch(function(err){
                console.log("*******Did not return Image Info ********");
                callback(err);
            });
        }
    function done(err, results){
        callback({
            userList: results.getUser,
            imageList : results.getData,
            user: results.verifyUser,
        });
    });
}

Instead of running the script on page-load, I want to call it when I submit an HTML form. How would I go about that?



via velkoon

No comments:

Post a Comment