Calling a local python script from javascript
Question: I have the following requirement:

1.Need to call a python script which resides locally, from javascript. It will
carry out some operations and return a xml file.

2.Then I need to return the xml file to the javascript.

3.The javascript will carry out the parsing of the xml and display the values
in the HTML.

Can you kindly give me some pointers on how to carry out steps 1 and 2?I have
tried to find answers to this but I haven't got a well defined solution.

Thanks, Sayan


Answer: Your browser **can't** (and know you how to) **execute Python functions**
/modules.  
What you want is to make an [AJAX
request](http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp).
Basically, you need to put a web server in front of your Python function and
then return the result of the function, here your XML file, when a certain URL
is called.  
There's a lot of lightweight web framework that should help you to setup a
quick web server to do that. For instance, [Flask](http://flask.pocoo.org/).
Here's an example, totally inspired from the homepage of Flask:

    
    
    from flask import Flask
    from yourmodule import function_that_return_xml
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        xml = function_that_return_xml()
        # make fancy operations if you want
        return xml
    
    if __name__ == "__main__":
        app.run()
    

Then, here you can call `http://localhost:5000` (default address, put it
online if you want other users to use it) to get your XML file.

