Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Python script from AJAX or JQuery

We have a requirement to run python scripts from Javascript. We have to pass an input String to the python script as an argument, and display the python output onto our web page.

Here is the Python code, this code works fine when I run this in my linux box: ./sample.py 12345, gives the output 12345 and ./sample.py would display no argument found

#!/usr/bin/env python

import os
import sys

if len(sys.argv) > 1:
  output = sys.argv[1]
else:
  output = "no argument found"

print "Hello World!!!"
print output

How do I access the 'param' from the ajax call in above python, and use that value as an argument?

Javascript:

$.ajax({
        type: 'POST',
        url: "scripts/sample.py",
        data: {param: xyz}, //passing some input here
        dataType: "text",
        success: function(response){
           output = response;
           alert(output);
        }
}).done(function(data){
    console.log(data);
    alert(data);
});

Any help would be greatly appreciated.

EDIT

As suggested, I am trying to get my code working using CGI.

#!/Python34/python

# Import modules for CGI handling 
import cgi, cgitb

# Create instance of FieldStorage 
data= cgi.FieldStorage()

# Get data from fields
output = data["param"]

# This will print to stdout for testing
print("Hello World!!!")
print(output)

Whenever the Ajax call is run, response is: [object HTMLDivElement], when opened in Firebug displays the entire text of the python script. What am I missing here?

like image 659
ritusmart Avatar asked Aug 16 '14 03:08

ritusmart


People also ask

Is AJAX or jQuery better?

jQuery uses ajax for many of its functions, but it nothing else than a library that provides easier functionality. With jQuery you dont have to think about creating xml objects ect ect, everything is done for you, but with straight up javascript ajax you need to program every single step of the ajax call.

How do I run a Python script from AJAX?

Call the script you want to call. Once flask is installed, Python run.py is enough. AJAX is to send an HTTP request to the background, the background is to receive the request, process and return the results, using Python as an HTTP server would be good, this has a good framework, flask is good you can try.

Does Python use AJAX?

Another way of using Ajax in Django is to use the Django Ajax framework. The most commonly used is django-dajax which is a powerful tool to easily and super-quickly develop asynchronous presentation logic in web applications, using Python and almost no JavaScript source code.

What is the difference between AJAX and jQuery?

AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser. It works on the browser or outside the browser also.


2 Answers

Your Ajax call will be a web request (http) to the python script, so your python script needs to listen on the port for such requests and respond back.

Above mentioned method is recommended, to use simple web server inside your sample ,py or you can use an existing ones such as Flask (or many others).

FYI - for your simple task I would not recommend Django (its a big and heavy web framework with lots of bells you do not need).

like image 66
Vlad Gulin Avatar answered Oct 16 '22 06:10

Vlad Gulin


Getting data from the fields should be done as follows:

# Get data from fields
output = data.getvalue("param")

Also, realized there are ^M characters issues while trying to execute, which I resolved using the vi editor.

like image 42
ritusmart Avatar answered Oct 16 '22 08:10

ritusmart