简体   繁体   中英

Running python script in IBM Watson node.js chatbot

I made a chatbot in Node.js with IBM Watson. I'm trying to run a python script in it but I'm not able to. I tried browserify and bundled up all dependencies in a .js file and called the script in the html page but still it isn't working.

var PythonShell = require('python-shell');

var options = {
    mode: 'text',
    args: 765
};

    PythonShell.run('pyt.py', options, function (err, results) {
        if (err) throw err;
        console.log('results: %j', results[0].toString());
    });

It's running on locally between a node.js and a python file. But, doesn't work when I deploy the chatbot on a server.

If you want to use the data returned from the Python script, you should use in your python script, try to put it:

print(dataToSendBack)
sys.stdout.flush()

And then Node can check your data with:

var spawn = require("child_process").spawn;
var pythonProcess = spawn('python',["path/to/script.py", arg1, arg2, ...]);

pythonProcess.stdout.on('data', function (data){
// Do something with the data returned from python script
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM