Showing updated content on the client

Posted by tazim on Stack Overflow See other posts from Stack Overflow or by tazim
Published on 2010-06-15T10:44:50Z Indexed on 2010/06/15 10:52 UTC
Read the original article Hit count: 217

Filed under:
|
|

Hi,

I have a file on server which is viewed by the client asynchronously as and when required . The file is going to get modified on server side . Also updates are reflected in browser also In my views.py the code is :

   def showfiledata(request):  
    somecommand ="ls -l > /home/tazim/webexample/templates/tmp.txt"    
    with open("/home/tazim/webexample/templates/tmp.txt") as f:  
         read_data = f.read()  
    f.closed  
    return_dict = {'filedata':read_data}  
    json = simplejson.dumps(return_dict)  
    return HttpResponse(json,mimetype="application/json")  

Here, entire file is sent every time client requests for the file data .Instead I want that only modified data sholud be received since sending entire file is not feasible if file size is large .

My template code is :

< html>  
< head>
< script type="text/javascript" src="/jquerycall/">< /script>  
< script type="text/javascript">  
    $(document).ready(function()  
    {  
         var setid = 0;  
         var s = new String();  
         var my_array = new Array();    
         function displayfile()  
         {
              $.ajax({  
                        type:"POST",  
                        url:"/showfiledata/",  
                        datatype:"json",  
                        success:function(data)  
                               {  

                                  s = data.filedata;  
                                  my_array = s.split("\n");  
                                  displaydata(my_array);  
                               }  
                    });  
         }  

         function displaydata(my_array)  
         {  
            var i = 0;  
            length = my_array.length;  
              for(i=0;i<my_array.length;i++)  
              {
                 var line = my_array[i] + "\n";
                 $("#textid").append(line);
              }
         }


         $("#b1").click(function()
         {
               setid= setInterval(displayfile,1000);
         });


         $("#b2").click(function()
         {
               clearInterval(setid);

         })
    });
< /script>  
< /head>  
< body>  
< form method="post">  
< button type="button" id="b1">Click Me< /button>< br>< br>  
< button type="button" id="b2">Stop< /button>< br>< br>  
< textarea id="textid" rows="25" cols="70" readonly="true" disabled="true">< /textarea>  
< /form>  
</body>  
</html>  

Any Help will be beneficial . some sample code will be helpful to understand

© Stack Overflow or respective owner

Related posts about python

Related posts about AJAX