-3

I'm adding extra functionality to an existing JS website, and Python works best to do the math that the program needs to do. What I need is for a user to submit information, which should then become input for my Python code, which runs and sends the output back to the site. I'm attempting to do this with an AJAX call, although I'm open to other options. What I can't figure out is what needs to happen to or around my Python code for it to accept input and send output. I'm missing the piece that would be running, ready to accept the AJAX call, run the Python, and send everything back. I've looked everywhere, and I saw a couple of (not very good) examples using CGI and such. Is that the best option? What would be the cleanest and simplest way to do this?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
lala
  • 37
  • 4

1 Answers1

1

You'll need a web server running. There are full-python web servers out there or you could make your own python code act as a server. Or you could use a PHP server and execute the python script with it, but if you've never done PHP I don't think it'll be advantageous.

EDIT:

I'd probably use the php exec function instead, as you can pass it an array.

<?PHP
$output = [];
exec("python myScript.py", $output);
// the $output array now contains all lines printed by the python script
?>

<p>
  The solution was <?PHP echo $output[0]; ?>.
</p>
Community
  • 1
  • 1
Domino
  • 6,314
  • 1
  • 32
  • 58
  • I think there might be a PHP server already running, if that makes things easier. If I use it to execute Python code, how do I pass input into the Python code and output back? – lala Mar 17 '15 at 15:14
  • @Sonya If the web page is serve by a PHP server, then you can put php code in your html pages if you put them in the server folder and change the extension to .php, and then this script should do the trick. You might have to replace "python" with the full path to the python interpreter. – Domino Mar 17 '15 at 15:49
  • Perfect. Thank you for understanding my confusion and helping. – lala Mar 17 '15 at 16:54