I'm trying to make a server that asks the user for a data, uses it as an argument in a python script, and shows the real-time output for the user in the browser. I'm using python-shell to call the python script and pass the arguments. Until now I was able to start the output of the python script on the screen, but after the program finished and not in real time .. Any suggestions?
server.js:
var express = require('express');
var app = express();
app.use(express.static('public'));
var http = require('http').Server(app);
var bodyParser = require("body-parser");
var PythonShell = require('python-shell');
app.use(app.router);
var port = process.env.PORT || 3000;
var msg = '';
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
http.listen(port, function() {
console.log('listening on *: ' + port);
});
app.get('/', function(req, res) {
res.render(__dirname + '/views/index.ejs', {output:''});
});
app.post('/', function(req, res){
var options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
args: [req.body.codigo]
};
var pyshell = new PythonShell('teste.py', options); //Roda script
pyshell.on('message', function (message) {
msg = msg + message;
});
pyshell.on('error', function(message){
msg = message;
});
pyshell.end(function(){
res.render('index.ejs', {output: msg});
msg = '';[]
});
});
index.ejs:
<html>
<head>
</head>
<script src="ejs.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script><body><h1>Terminalremoto</h1><formmethod="post" action="/">
Robo: <br>
<input type="text" name="codigo"><br>
<input type="submit" value="Submit">
</form>
<textarea name="texarea" rows="10" cols="50" placeholder = "Saida: "> <%= output %> </textarea>
</body>
</html>