-2

I am trying to make a webpage where I enter a latitude and longitude and a python program will process the latitude and longitude in the form of a graph. For simplicity sake, in this question I just have the python program just repeat the latitude and longitude because I am wanting to know how to run the python program in the context of the html and javascript code and pass the arguments from the html form (latitude and longitude) into the python script. Below is the html code:

<html>

<head>

<script>

    function set_iterations()
    {
       var count = 0;
       var lat = document.getElementById("lat").value;
       var lon = document.getElementById("lon").value;
       document.write("latitude is " + lat + " and longitude is " + lon + "</br>"); 
      $.ajax({
         type: "POST"
         url: "testmaker.py" + lat + lon
       });
   }

</script>
</head>

<body>
       <td style='border-right:none' align='center'>Latitude: </td>
       <td style='border-right:none' align='center'><input type=text id='lat' size=10></td>
       <td style='border-right:none' align='center'>Longitude: </td>
       <td style='border-right:none' align='center'><input type=text id='lon', size=10></td>
       <br> 
       <button style="width:200px;margin-top:5px;margin-bottom:20px;"   onClick="set_iterations();">Submit</button>

</body>
</html>

The python program is this:

  #!/usr/bin/python

  import sys

  latitude = sys.argv[1]
  longitude = sys.argv[2]

  print "Using coordinates: ",latitude,",",longitude

How should I modify this program so that the submit button on the form would call the python program with the entries in the form being command line arguments for the python program?

"

jms1980
  • 1,017
  • 1
  • 21
  • 37
  • 1
    I don't believe you can call the script directly from UI without a web-server in between (Let's say nodeJS for an instance). You can write an API in nodeJS and then call python from there! – TechnoCorner Jan 17 '17 at 21:42
  • Make sure your Python script is setup properly and able to return a response. Also, possible duplicate of: http://stackoverflow.com/questions/32288722/call-python-function-from-js – MannfromReno Jan 17 '17 at 21:43
  • Not quite a duplicate. I am also dealing with command line arguments, which the other post is not dealing with. – jms1980 Jan 17 '17 at 21:45
  • You need to add more information, are you getting console errors when the submit button is clicked? Can you navigate to this python script? Is your path correct for the script? – MannfromReno Jan 17 '17 at 21:46

1 Answers1

0

You need a comma between arguments in your ajax call:

$.ajax({     
    type: "POST",         
    url: "testmaker.py" + lat + lon
});

You need a web server of some sort. As a start, you can write a super simple server in Node:

server.js
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('form.html');

http.createServer(function (req, res) {
  console.log("request made") ;
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(index);
}).listen(9615);

Start it:

node server.js

Then go to http://127.0.0.1:9615/ from your browser

Now you need to do something in the server to respond to a call to your 'testmaker' url...