PDA

View Full Version : Calling into Python



Liminality
24 Apr 2009, 1:52 PM
I have an XMLReader that is loading a data store into an editor-grid panel. I was wondering how I could go about--after the user has navigated away from the page or ended their session--writing the new store out to the .xml file. In other words, I want to update the server's .xml file with the updated information as reflected in the edited store. I was looking into AJAX to do this by calling a python script. I wanted to have the python script accept the edited store as a parameter, then simply parse it, and overwrite the old .xml file. Unfortunately, I am not sure how I would go about passing the store as a parameter to the python script, as in the examples, I have seen only strings passed. Can anyone here shed some light on this or, perhaps, suggest another approach to the problem?

michaelc
24 Apr 2009, 5:25 PM
I have an XMLReader that is loading a data store into an editor-grid panel. I was wondering how I could go about--after the user has navigated away from the page or ended their session--writing the new store out to the .xml file. In other words, I want to update the server's .xml file with the updated information as reflected in the edited store. I was looking into AJAX to do this by calling a python script. I wanted to have the python script accept the edited store as a parameter, then simply parse it, and overwrite the old .xml file. Unfortunately, I am not sure how I would go about passing the store as a parameter to the python script, as in the examples, I have seen only strings passed. Can anyone here shed some light on this or, perhaps, suggest another approach to the problem?

First question I would have is python defined to your webserver ?
if it is you can call the process directly from your front-end.

If python is not defined to your web server then you would need a middle layer say PHP and from there make a system call.
$command = proc_open($pythonProcess, $descriptorspec, $pipes);



Python now understands JSON and XML so why not just pass the XML string itself.

I would suggest you use JSON and make your life a lot easer, this way the front-end can convert the results to JSON and pass it back, python can then convert it back into a python object and your life is a lot easyer.


I have all of my backend folks converted to JSON now, both the Python ones and the Java types.

Liminality
28 Apr 2009, 8:08 AM
Thanks for the suggestion.. I've read the documentation, but I'm not quite sure what JSON is. I know it revolves around retrieving data from a server, but what is the advantage to straight .xml? If I follow you correctly, you're saying it's possible to read in the data from the server, edit the data, and then write it back out and save it back on the server. And this can be accomplished with JSON how? I guess I'm just not sure what data types are necessary... I also don't understand what you mean by the xml string. I am not editing the xml string itself, rather the data in the store, so I'm not quite sure how this would help or matter. After years of straight C/C++/Java programming, all of this web development stuff is new to me. But thanks a lot for your reply.

evant
28 Apr 2009, 8:11 AM
Google is your friend: http://www.google.com.au/search?hl=en&q=json&btnG=Google+Search&meta=&aq=f&oq=

tryanDLS
28 Apr 2009, 8:23 AM
http://json.org/

Liminality
28 Apr 2009, 8:28 AM
Ok, great. I know what JSON is now. However, knowing generally what something is, and how to utilize it in the very specific context in which I need it is two different things. If I had a python script that parsed a file (of any format), I could create a JSON object with it. And feel free to correct me at any step, if I'm wrong in any of these assumptions. From there, I could retrieve the data from the server in the .js code (which should be faster than .xml, apparently). After all of the necessary editing has been done, I can pass the JSON object back to another python script that would re-write the updated file. Is this all correct?

evant
28 Apr 2009, 9:54 AM
It's no different from any other web technologoy, it's just you're formatting the data in a particular way.

So the short answer is, yes. Receive json from the server. Modify it on the client. Send it off to the server. There's plenty of examples of this around.

michaelc
29 Apr 2009, 6:19 AM
Ok, great. I know what JSON is now. However, knowing generally what something is, and how to utilize it in the very specific context in which I need it is two different things. If I had a python script that parsed a file (of any format), I could create a JSON object with it. And feel free to correct me at any step, if I'm wrong in any of these assumptions. From there, I could retrieve the data from the server in the .js code (which should be faster than .xml, apparently). After all of the necessary editing has been done, I can pass the JSON object back to another python script that would re-write the updated file. Is this all correct?

it's far more simple then that. JSON is like XML in javascript format http://www.json.org/
Python has an encoder/decoder http://docs.python.org/library/json.html
so you can create a python object and using json.dump or encode to convert to JSON. you can also do the reverse. when you receive a JSON object you can convert it to a PYTHON object using JSON.decode this is avaliable in 2.6 if your running an earlyer version then you may need to add a package like simplejson http://pypi.python.org/pypi/simplejson/

the beauty of it is how simple the the format is you have elements and arrays you name them and that's about it. using the encode/decode you can create a python object that has the data you need in the format you want for your fron-end and just convert and send, job done. as I said I have converted the backend python folks completely infact there now using JSON to interact with there services. I didn't expect that to occur.

Liminality
30 Apr 2009, 7:10 AM
Thanks for the relevant response, michaelc. I have several more questions, if you don't mind. Firstly, I intend to use an Ajax request to send the data to the python script... I have the store that has successfully loaded, using a JsonReader. Whenever the client exits, I want to send the data to the python script. What do I pass as the param to the python script? I mean, how do I get the actual altered data back out of the store in a format that is either json/can be converted to json? I can deal with the data on the other side, but I'm just not sure what to pass. Thanks again for your time...

michaelc
1 May 2009, 6:50 AM
Thanks for the relevant response, michaelc. I have several more questions, if you don't mind. Firstly, I intend to use an Ajax request to send the data to the python script... I have the store that has successfully loaded, using a JsonReader. Whenever the client exits, I want to send the data to the python script. What do I pass as the param to the python script? I mean, how do I get the actual altered data back out of the store in a format that is either json/can be converted to json? I can deal with the data on the other side, but I'm just not sure what to pass. Thanks again for your time...

if python is defeined to your webserver, I our case we don't and have php act as the controller. the your ajax call will pass it as a get or a post based on the method. pretty simple. I would suggest you do a tiny poc and it will all become clear. just

var myJSONText = JSON.stringify(myObject, replacer);

http://www.json.org/js.html

covert you object to json ajax call, pick up the get in python convert to an object and run with it.

hope that helps.