Saturday, 18 March 2017

Python output on HTML page

I have a python program and I want to print the output of that program on an HTML page using Node JS. The python script is being called using 'child_process'. I want to print the value of 't' on HTML page. Is there any way to do it?

script.py

import time

t=0
def main():
    while 1:
        t =t+1
        print t
        time.sleep(2)

# Start process
if __name__ == '__main__':
    main()

Here I want to print the value of 't' after every 2 seconds.

app.js

var sys =require('sys');
var myPythonScript ="script.py";
var path =resolve("C:\\Python27\\python.exe");
var pythonExecutable = path;

const spawn = require('child_process').spawn;
const scriptExecution = spawn(pythonExecutable, [myPythonScript]);

scriptExecution.stdout.on('data', (data) => {
    console.log(data);
});
});

This JS function is called when a button is clicked on the HTML page.



via Amit Naik

No comments:

Post a Comment