Ciao,
sto sviluppando su Intel Edison un applicativo web che gira du Nodejs.
Dal browser apro una pagina corrispondente all'indirizzo IP della Edison, per esempio http://192.168.x.xxx:5555/index.htm, e nella pagina c'è un pulsante che cliccato mi deve far partire degli script python e che deve mostrare i risultati (dei grafici) nella pagina web.
Il lancio dello script (ne ho provato uno semplice cioè un print) va bene (se metto il plot addio nn funaziona) e nella console ho la scritta che chiedo di stampare ma quello che non riesco a fare è come redirigere l'output, la scritta del print, sul browser.
Ho dato una occhiata a flask ma non riesco a farlo funzionare, ricevo diversi errori.
La mia pagina html, index.htm:
codice:
<!DOCTYPE html>
<html lang="us">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Elaborazione dati ECG-GSR</title>
<link rel="stylesheet" href="css/style.css">
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script>
//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');
});
});
</script>
</head>
<!--------------------------------------------------------------------------->
<!---------------------------------- BODY ----------------------------------->
<!--------------------------------------------------------------------------->
<body>
<div id="page">
<div class="page_line">
<span class="welcome"><br>Launch script ECG-GSR</br></span>
<div class="page_line">
<button id="launch_on" class="io_btn-s">Launch Script</button>
</div>
</div>
</body>
</html>
il mio file js:
codice:
//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('-------------------------------------------------------------');
e lo script python di test:
codice:
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)
Spero qualcuno mi può dare una dritta.
Grazie