Intel® Xeon® Processor and Server Products
Intel® Xeon® Processors, Data Center Products including boards, integrated systems, and RAID Storage
Announcements
For support on Altera products please visit the Altera Community Forums.
5294 Discussions

Launch python script in html page

GRicc
Novice
21,105 Views

Hi,

I created a node js project into my Intel XDK. I use an Intel Edison and I want to use it as a web server.

I want to create an index.html page where there is a button and when the button is pressed it is launched a python script. I'm new on Edison and I have no idea to start with python on Edison.

Any help is appreciated.

Best regards.

Giuseppe

22 Replies
Matthias_H_Intel
Employee
2,156 Views

not quite sure what you're seing. Following is running just fine from XDK and starting the python blink example (same as blink-io8.py but changed to io13 to have on board led blinking).

As mentioned, I use the relative paths as per below:

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

pythonShell.run('../../usr/share/mraa/examples/python/blink-io13.py', function(err){

if (err) throw err;

console.log('finished');

});

Only drawback: Stopping the app from XDK wouldn't stop the python script running. But you'll find the python process running on your target

# ps | grep python

628 root 7384 S python ../../usr/share/mraa/examples/python/blink-io13.py

0 Kudos
GRicc
Novice
2,156 Views

Hi guys,

I am happy to announce that it has found a way to run the script python. I left python-shell and I installed another library: python-runner and it works in xdk.

My main.js is:

//Required

var mraa = require('mraa'); //require mraa

var Cylon = require ('cylon');//require cylon

var querystring = require('querystring');

var express = require('express');

var Python = require("python-runner");

var fs = require('fs');

/////////////////////////////////////////////////START//

//Set Public folder

var app = express();

var path = require('path');

app.use(express.static(__dirname + '/public'));

app.get('/', function (req, res) {

res.sendFile(index.htm);

// res.sendFile(path.join(__dirname + '/index.htm'));

});

//Start Listening

var server = app.listen(5555, function(){

var host = server.address().address;

var port = server.address().port;

console.log('Example app listening at http://%s:%s', '192.168.0.139', port);

});

//Start dialog with index.html page

var io = require('socket.io').listen(server); // to dialog with the webpage

io.sockets.on('connection', function(socket){

//recieve client data with CYLON

socket.on('launch_on', function(){

Python.execScript(

__dirname + "/example.py",

{

bin: "python2",

args: [ "argument" ]

}

)

.then(function(data){

console.log(data);

res.writeHead(200, {'Content-Type': 'text/plain'});

res.end('ok');

})

.catch(function(err){

console.log("Error", err);

res.writeHead(500, {'Content-Type': 'text/plain'});

res.end('error');

});

});

});

console.log('-------------------------------------------------------------');

console.log('The express app is running');

console.log('-------------------------------------------------------------');

and my esample file is:

import numpy as np

print('Carma Carpooling')

print('Get there together')

a=np.array([1,2,3,4,5])

b=np.array([10,23,45,65,78])

print(a)

print(b)

print('somma di a e b')

data=a+b

print(data)

So, when I press the button in the browser in the xdk console I have:

Carma Carpooling

Get there together

[1 2 3 4 5]

[10 23 45 65 78]

somma di a e b

[11 25 48 69 83]

Error [ReferenceError: res is not defined]

I don't understand the error..but my next task is to send the data of python script (print, plot,...) to the browser.

Thanks.

Giuseppe.

0 Kudos
Reply