Server Products
Data Center Products including boards, integrated systems, Intel® Xeon® Processors, RAID Storage, and Intel® Xeon® Processors
4761 Discussions

Launch python script in html page

GRicc
Novice
4,257 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
Salem_W_Intel1
Employee
2,459 Views

I will be moving this to the corresponding /community/tech/edison support group.

0 Kudos
Carlos_M_Intel
Employee
2,459 Views

Hi Giuseppe,

Having your python code in your board, you can use a system call in order to start it.

For example if you have a code named blink.py in /home/root you can create a Javascript code to run it with:

var SC = require('child_process')

SC.exec("python /home/root/blink.py")

I hope this helps.

Regards,

 

Charlie
0 Kudos
GRicc
Novice
2,459 Views

Hi Charlie,

I tried with your suggestion but it doesn't work.

This is my mani.js:

//Required

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

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

var querystring = require('querystring');

var express = require('express');

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

//Set Public folder

var app = express();

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

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

res.sendFile(index.html);

});

//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.179', 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('client_data', function(data){

});

//recieve client data

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

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

var SC = require('child_process')

SC.exec("python test.py")

console.log('script launched');

});

});

and this is my HTML page with the button (when it is pressed it is launched the python script):

<!DOCTYPE html>

Elaborazione dati ECG-GSR

https://cdn.socket.io/socket.io-1.3.5.js https://cdn.socket.io/socket.io-1.3.5.js">

https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js">

//Connect with the server

var socket = io.connect();

//Send data to the server

$(document).ready(function(){

socket.emit('launch_on', {'message': 'launch_on'});

$('# launch_on').click(function(e){

socket.emit('launch_on');

});

});

<!--------------------------------------------------------------------------->

<!---------------------------------- BODY ----------------------------------->

<!--------------------------------------------------------------------------->

Launch python script

Lancia Script

Hoping this is helpful for a solution.

Thanks.

0 Kudos
Matthias_H_Intel
Employee
2,459 Views

what does "does not work" mean? do you get an error message?

Maybe you need to put the full path "/usr/bin/python"?

0 Kudos
GRicc
Novice
2,459 Views

Hi,

"does not work" it means when I press the button on the HTML page the python script don't show any result.

My python script is a simple print.

This is my HTML page in the browser.

0 Kudos
Carlos_M_Intel
Employee
2,459 Views

Hi Giussepe,

Do you have updates on this?

I saw on your code that you are not using Python-Shell, have you tried with something like:

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

PythonShell.run('test.py',function(err){

if (err) throw err;

console.log('finished');

});

console.log('script launched');

Regards,

Charlie

0 Kudos
GRicc
Novice
2,459 Views

Hi Charlie,

I try with your suggestion:

//recieve client data

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

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

PythonShell.run('public/test.py',function(err){

if (err) throw err;

console.log('finished');

});

});

but I receive the errors shown below. The file test.py is located in the public fold.

Some idea to resolve these errors?

Thanks.

giuseppe

The express app is running

-------------------------------------------------------------

Example app listening at http://192.168.0.231:5555 http://192.168.0.231:5555

ERROR: /home/root/.node_app_slot/main.js:36

ERROR: if (err) throw err;

ERROR: ^

ERROR: Error: python: can't open file 'public/test.py': [Errno 2] No such file or directory

at PythonShell.parseError (/home/root/node_modules/python-shell/index.js:173:17)

at ChildProcess. (/home/root/node_modules/python-shell/index.js:80:28)

at ChildProcess.emit (events.js:98:17)

at Process.ChildProcess._handle.onexit (child_process.js:820:12)

0 Kudos
Carlos_M_Intel
Employee
2,459 Views

Hi Giuseppe,

Have you tried to put the entire location of the file instead of using public/test.py?

Also, is the same outcome when you put the test.py in the same folder of index.html and your main.js?

What is the output of running:

node --version

npm --version

configure_edison –version

Kind regards,

Charlie

0 Kudos
GRicc
Novice
2,459 Views

Hi Charlie,

putting test.py in public or in the same directory of main.js don't changes anything. I have this error:

Example app listening at http://192.168.0.128:5555 http://192.168.0.128:5555

ERROR: /home/root/.node_app_slot/main.js:39

ERROR: if (err) throw err;

ERROR: ^

ERROR: Error: python: can't open file 'test.py': [Errno 2] No such file or directory

at PythonShell.parseError (/usr/lib/node_modules/python-shell/index.js:173:17)

at ChildProcess. (/usr/lib/node_modules/python-shell/index.js:80:28)

at ChildProcess.emit (events.js:98:17)

at Process.ChildProcess._handle.onexit (child_process.js:820:12)

The results of the commands suggested:

root@edison:~# node --version

v0.10.38

root@edison:~# npm --version

3.3.10

root@edison:~# configure_edison --version

159

Thanks for your attention.

<p style="font-family: Menlo, 'DejaVu Sans Mono', Consolas, 'Liberation Mono', Courier, 'Ubuntu...
0 Kudos
Carlos_M_Intel
Employee
2,459 Views

Hi giuseppe,

I would like to know if you are running the same code as the one is attached.

Also, could you post a screenshot of what you get when you type ls -la in the folder where you have the main.js, the index.html and test.py?

Regards,

Charlie

0 Kudos
GRicc
Novice
2,459 Views

Hi Charlie,

the code used is the same of my past post.

This is a screen shot of my Intel XDK IoT Edition when I give the ls -la command.

Thanks.

Giuseppe.

0 Kudos
Matthias_H_Intel
Employee
2,459 Views

have you tried the debugger yet?

0 Kudos
Matthias_H_Intel
Employee
2,459 Views

I just tested it: apparently absolute paths (like "/root/test.py") don't seem to work but rather relative paths to the upload folder /opt/xdk-daemon. I.e. "/root/test.py" would translate into "../../root/test.py"

0 Kudos
GRicc
Novice
2,459 Views

Thanks for your replies.

I put my test.py in the root directory and I tried with:

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

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

PythonShell.run('../../root/test.py', function(err){

if (err) throw err;

console.log(err);

console.log('finished');

});

as suggested but I have these errors:

Example app listening at http://192.168.0.154:5555 http://192.168.0.154:5555

ERROR: /home/root/.node_app_slot/main.js:39

ERROR: if (err) throw err;

ERROR: ^

ERROR: Error: python: can't open file '../../root/test.py': [Errno 2] No such file or directory

at PythonShell.parseError (/usr/lib/node_modules/python-shell/index.js:173:17)

at ChildProcess. (/usr/lib/node_modules/python-shell/index.js:80:28)

at ChildProcess.emit (events.js:98:17)

at Process.ChildProcess._handle.onexit (child_process.js:820:12)

I have no idea...

Thanks.

giuseppe

0 Kudos
Carlos_M_Intel
Employee
2,459 Views

Hi Giuseppe,

If the error appears only when you press the button on the website. Try to use path, like:

var path = require('path');

 

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

 

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

 

});

 

Also try by setting test.py as an executable using the Terminal Console with chmod +x test.py

Without pressing the button, do you receive error messages? Does the website works fine until you press the button?

Regards,

Charlie

0 Kudos
GRicc
Novice
2,459 Views

Hi guys,

thanks for your replies.

With the suggested code in the posted question I am not able to have the files list, I have many errors.

The test.py in root directory I set the 777 permission but don't changes anything.

After run the script (without pressing the button in the index.htm page) I have the errors:

Upload Complete

App is already running

-------------------------------------------------------------

The express app is running

-------------------------------------------------------------

Example app listening at http://192.168.0.204:5555 http://192.168.0.204:5555

ERROR: /home/root/.node_app_slot/main.js:45

ERROR: if (err) throw err;

ERROR: ^

ERROR: Error: python: can't open file '../test.py': [Errno 2] No such file or directory

at PythonShell.parseError (/usr/lib/node_modules/python-shell/index.js:173:17)

at ChildProcess. (/usr/lib/node_modules/python-shell/index.js:80:28)

at ChildProcess.emit (events.js:98:17)

at Process.ChildProcess._handle.onexit (child_process.js:820:12)

Pressing the button don't happen anything.

I have no idea how to resolve all these problems.

Thanks.

0 Kudos
Carlos_M_Intel
Employee
2,459 Views

Hi Giuseppe,

I tried with this example: http://socket.io/get-started/chat/ Socket.IO — Chat Using the 159 version, I was able to run a similar code than the one you are using, but you will need to work on the reception routine to run the python script. Take a look at the following code.

var app = require('express')();

var http = require('http').Server(app);

var io = require('socket.io')(http);

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

res.sendFile(__dirname + '/index.html');

});

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

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

PythonShell.run('test.py',function(err){

if (err) throw err;

});

});

  1. http.listen(3000, function(){

console.log('listening on *:3000');

});

If you have the main.js, index.html and test.py in the same folder you shouldn't have problems, you will just need to access to the IP address of your board on your web browser (like 192.168.10.123:3000) . I hope this gives you an idea for another approach.

Regards,

Charlie

0 Kudos
GRicc
Novice
2,459 Views

Hi, Charlie,

there is no way to run correctly my script.

I try to reinstall python shell but as the first installation, it seems to don't work:

root@edison:~# npm install python-shell

/home/root

`-- python-shell@0.2.0

npm WARN ENOENT ENOENT, open '/home/root/package.json'

npm WARN EPACKAGEJSON root No description

npm WARN EPACKAGEJSON root No repository field.

npm WARN EPACKAGEJSON root No README data

npm WARN EPACKAGEJSON root No license field.

I used the code posted but I have the same error of my original script:

If I want to modify the permissions of the test.py, where is loaded on Intel Edison, in which directory?

Best regards.

Giuseppe.

0 Kudos
Carlos_M_Intel
Employee
2,261 Views

Hi Giussepe,

I was able to replicate your issue, this is only happening while using the XDK, if you use the Linux Terminal the error doesn't come up.

I tried changing the package.json in order to install the dependencies, no success

{

"name": "blankapp",

"description": "",

"version": "0.0.0",

"main": "main.js",

"engines": {

"node": ">=0.10.0"

},

"dependencies":{

"express": "=4.13.3",

"socket.io": "=1.3.7",

"python-shell":"=0.2.0 "}

}

I added the index.html and test.py in /home/root/.node_app_slot, then I ran the code with the XDK, no success

 

 

I haven't been able to remove the error while using the XDK, it seems that the xdk-daemon is not able to open python to run the code while running the JS script.

An alternative could be to use the Linux Terminal. If you open a session and go to a folder where the main.js, index.html and test.py are located you will be able to run node main.js without errors.

Regards,

Charlie

0 Kudos
Reply