Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have seen this topic: ajax request to python script where they were trying to do exactly what I need but there is one information missing.

$.post('myPythonFile.py',{data}, function(result){ //TODO } );

Now my problem is: how do I call a certain function which is inside myPythonFile.py? Is there a way to specify the name of the function and to give it my input data? Thank you very much for your time.

share|improve this question
    
You probably can't call the function directly, but you could have a value in the posted data that you do an if-else on inside the python code. – Stian May 2 '13 at 13:00
    
So the python file has to be a function itself instead of containing functions. Is that correct? – user2343163 May 2 '13 at 13:09
    
I see you are using Django, I would advice you to read the documentation to understand how frameworks like that work.. – Stian May 2 '13 at 13:15
    
There is no in-built, straight out-of-the-box support for Python.. It really depends on how your server is configured and what you are running on it. Therefore it's difficult to answer this question unless you specify which framework/server software you use. – MMM May 2 '13 at 13:18
    
First send the function name you need to django. The name can be plain text. Then you can call globals() in python, it will return a dict which stores all name in your current module. For example, you want to call Myfunction: send text {text:"Myfunction"} in json form, then in your py call globals()[request.text](). it should be called. – Herrington Darkholme May 2 '13 at 13:51

Ajax calls making HTTP requests,so you need to have a HTTP server which handles your requests as it is seen in the other question. (There is CGI which provide HTTP request handling). In Python you can use DJango, Bottle, CGI etc to have HTTP request handling. to call python function from javascript.

Edited :

in your url.py you should define your api url;

(r'^myAPI', 'myAPI'),

and on this url you should have a web API in views.py. It can be like ;

def myAPI(request):
    callYourFunction();

and you can call it from Javascript now. You can use JQuery for AJAX request;

 $.ajax({
            type:"GET",
            url:"/myAPI",
            contentType:"application/json; charset=utf-8",
            success:function (data) {     
            },
            failure:function (errMsg) {
            }
        });

The HTTP method type does not matter, if you only wanna run a Python script. If you wanna send data from javascript to Python you can send the data as JSON to Python as POST method.

share|improve this answer
    
I am using Django but I am not really familiar with it. Should I write something in the url.py file? Sorry but I'm new to this kind of stuff. – user2343163 May 2 '13 at 13:10
    
I edited thq answer please check it. – Ahmet DAL May 2 '13 at 13:26
    
Thanks Ahmet DAL. I will work on this path but it will take me sometime to figure out how things work. – user2343163 May 2 '13 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.