我已根据this site上的说明安装了python-shell
,并且能够在页面加载时让此代码在 test.ejs 上运行:
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');
});
我将其设置为 test.ejs 页面加载,将其放入 test.js 。 关于 test.ejs 页面的 test.js 部分如下所示:
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,
});
});
}
我不想在页面加载时运行脚本,而是在提交HTML表单时调用它(并在表单的参数中传递)。我该怎么做呢?