views:

332

answers:

3

I've been programming Python a while, but DJango and web programming in general is new to me.

I have a very long operation performed in a Python view. Since the local() function in my view takes so long to return, there's an HTTP timeout. Fair enough, I understand that part.

What's the best way to give an HTTPresponse back to my users immediately, then dynamically show the results of some python code within the page? I suspect the answer may lie in AJAX but I;m not sure how AJAX on the client can be fed from Python on the server, or even the modules one would commonly use to do such a thing.

+2  A: 

One way is to submit the task using AJAX/JS or the normal way, start it in background in your view and return immediately. Then use AJAX/JS on client side to periodically check if task is done. If it's done reload the page or provide a link to the client.

CLIENT "Please start a task using this data."-> SERVER

CLIENT <- "Task started!" SERVER

CLIENT "Done?"-> SERVER

CLIENT <- "Nope." SERVER

CLIENT "Done?"-> SERVER

CLIENT <- "Yep, here's a link where you can view results" SERVER

While sending data from server to client without client asking for it is possible, well kind a, (the technology is called Comet) it isn't really necessary in your case.

Maiku Mori
+6  A: 

Ajax doesn't require any particular technology on the server side. All you need is to return a response in some form that some Javascript on the client side can understand. JSON is an excellent choice here, as it's easy to create in Python (there's a json library in 2.6, and Django has django.utils.simplejson for other versions).

So all you need to do is to put your data in JSON form then send it just as you would any other response - ie by wrapping it in an HTTPResponse.

Daniel Roseman
+1  A: 

I'm not sure if this is what you are looking for, but maybe this question (How to implement a minimal server for AJAX in Python?) is helpful. In my answer I give a minimal example (which is not very well written, for example I would now use jquery...).

Edit: As requested by the OP, here is an example for the frontend with JQuery. Note that I'm no expert on this, so there might be issues. This example is supposed to work with a JSON-RPC backend, like this one.

<html>
<head>

<title>JSON-RPC test</title>

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="json2.js"></script>

<script type="text/javascript">

function test_button() {
    var data = $("[name=test_text]").val();
    var json_object = {"method": "power",
                       "params": [parseInt(data), 3],
                       "id": "test_button"};
    var json_string = JSON.stringify(json_object);
    $.post("frontend.html", json_string, test_callback, "json")
}

function test_callback(json_object) {
    $("#test_result").text(json_object.result.toString());
}

</script>

</head>
<body>

<input type="text" name="test_text" value="2" size="4">
** 3 =
<span id="test_result">0</span>
<input type=button onClick="test_button();" value="calc" title="calculate value">

</body>
</html>
nikow
If someone did have a complete Python on the server (using Django's DB models) and JQuery on the client example, I would have their babies.
nailer
I can't help you with Django, but using JQuery on the client is no problem, see my updated answer.
nikow