Sunday 19 March 2017

Displaying Javascript variables in HTML

app.js

app.route('/conn').post(function (req,res,next) {

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

        var uint8arrayToString = function(data){
        return String.fromCharCode.apply(null, data);
        };

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

    // Handle normal output
    var chunk =' ';
    scriptExecution.stdout.on('data', function(data) {
        chunk +=data;
    });
    scriptExecution.stdout.on('data', function(data) {
        console.log(chunk);
    });

    // Handle error output
    scriptExecution.stderr.on('data', (data) => {
        // As said before, convert the Uint8Array to a readable string.
        console.log(uint8arrayToString(data));
    });

    scriptExecution.on('exit', (code) => {
        //console.log("Process quit with code : " + code);

    });
    res.write('<h2>chunk</h2>');
    // End data write
    //scriptExecution.stdin.end();
    });

upload.html

<html>
<form action ="/file" method ="post">

    MAC Address:<br>
  <input type="text" name="macadd" id="macadd"><br>

  Percentage:<br>
  <input type="text" name="percent" id="percent"><br>


<input type="submit"  value='Submit' id="upload">
<br>
<input type="reset" value="Reset">
</form>
<input type=button onClick="location.href='/home'" value='Home'><br>
<form action ="/conn" method= "post">
<input type="submit" value='Show Users'><br>
</form>

</html>

script1.py

import sys,time

def main():
    t=0
    while t!=5:
        t =t+1
        print t


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

The value of t is getting stored in the 'chunk' variable and through console.log() I am able to see the value of t on the console. I want to display these values on the upload.html page. How do I do this? I tried using res.render and res.write function but nothing got displayed. Any way to do this?



via Amit Naik

No comments:

Post a Comment