Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  161] [ 7]  / answers: 1 / hits: 13686  / 10 Years ago, fri, february 21, 2014, 12:00:00

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


More From » python

 Answers
4

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(/)
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.


[#47517] Thursday, February 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;