1 2 Previous Next 22 Replies Latest reply on Dec 17, 2015 4:53 AM by GiuseppePegasus

    Launch python script in html page

    GiuseppePegasus

      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

        • 1. Re: Launch python script in html page
          Salem_Intel

          I will be moving this to the corresponding support group.

          • 2. Re: Launch python script in html page
            CMata_Intel

            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

            • 3. Re: Launch python script in html page
              GiuseppePegasus

              Hi Charlie,

               

               

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

               

              This is my mani.js:

               

               

              <code>

               

              //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');

              });

              });

               

               

              </code>

               

               

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

               

               

              <!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">Launch python script</span>

                      </div>

               

                      <div class="page_line">

                          <button id="launch_on" class="io_btn-s">Lancia Script</button>

                      </div>          

                  </div>

              </body>

              </html>

               

               

              Hoping this is helpful for a solution.

               

              Thanks.

              • 4. Re: Launch python script in html page
                mhahn

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

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

                • 5. Re: Launch python script in html page
                  GiuseppePegasus

                  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.

                  ScreenHunter_39 Nov. 05 14.46.jpg

                  • 6. Re: Launch python script in html page
                    CMata_Intel

                    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

                    • 7. Re: Launch python script in html page
                      GiuseppePegasus

                      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                                                                                                                                     

                                                                                                                                                                                                             

                      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.<anonymous> (/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)

                      • 8. Re: Launch python script in html page
                        CMata_Intel

                        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

                        • 9. Re: Launch python script in html page
                          GiuseppePegasus

                          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                                                                                                                                     

                                                                                                                                                                                                                 

                          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.<anonymous> (/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.

                          Best regards.

                          giuseppe

                          • 10. Re: Launch python script in html page
                            CMata_Intel

                            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

                            • 11. Re: Launch python script in html page
                              GiuseppePegasus

                              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.

                              iot_intel.jpeg

                              Thanks.

                              Giuseppe.

                              • 12. Re: Launch python script in html page
                                mhahn

                                have you tried the debugger yet?

                                • 13. Re: Launch python script in html page
                                  mhahn

                                  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"

                                  • 14. Re: Launch python script in html page
                                    GiuseppePegasus

                                    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                                                                                                                                     

                                                                                                                                                                                                                           

                                    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.<anonymous> (/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

                                    1 2 Previous Next