Skip to content Skip to sidebar Skip to footer

Calling A Local Python Script From Javascript

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 r

Solution 1:

Your browser can't (and know you how to) execute Python functions/modules. What you want is to make an AJAX request. 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. 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("/")defhello():
    xml = function_that_return_xml()
    # make fancy operations if you wantreturn 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.

Post a Comment for "Calling A Local Python Script From Javascript"