carol Alex
carol Alex

Reputation: 353

Django 1.9 show a countdown for HttpResponse/Render response

I'm writing a code where users can manage servers (AWS instances) from a browser. The servers take a while to start up and I would like to do a live countdown so users know how long to wait for before all the servers are turned on.

This is my views.py code

def startservers(request, question_id):
    inst() #updates all instance status
    if instance1[0].state in 'stopped':
        instance1[0].start()
    if instance2[0].state in 'stopped':
        instance2[0].start()
    if instance3[0].state in 'stopped':
        instance3[0].start()
    if instance4[0].state in 'stopped':
        instance4[0].start()
    for i in range (10,0,-1):
        print 'Wait for %d seconds \r' % i,
        sys.stdout.flush()
        time.sleep(1)
    check() #makes sure all instances are running
    text1 = 'WebServer-Test %s' % instance1[0].state
    text2 = 'Gluster %s' % instance2[0].state
return render(request, 'awsservers/startservers.html', {'webstatus': text1, 'glu1status': text2})

I'm not sure how I should parse the countdown value to

  return render(request, 'awsservers/startservers.html', {'webstatus': text1, 'glu1status': text2})

To understand better what I mean by a live countdown, you can just run this section of the code on a console to see the output it produces.

for i in range (10,0,-1):
        print 'Wait for %d seconds \r' % i,
        sys.stdout.flush()
        time.sleep(1)
print '' #without this, next line prints onto the same line as "Wait for.."
print "Server1 running"
print "Server 2 running"

Thanks for the help!

If anyone knows a much better way to put up a countdown, do let me know!

Upvotes: 0

Views: 279

Answers (1)

Resley Rodrigues
Resley Rodrigues

Reputation: 2288

One simple way to do this is via ajax. Using javascript make a call to your method every second and display the response. In this case you probably don't need a loop on the server side and can just return the state... If the server is started, display the appropriate text, if it is not show the countdown.

The JavaScript code will be something like this

var i = 10;
var timer = window.setInterval(function(){
  $.ajax({
    url: // your url
    data: // any parameters you need to pass as JSON
    success: function(response){
        if (response == 'started'){
            $('#server_status').text('Wait for ' + i + ' seconds')
            i--;
        }else
            $('#server_status').text('running')
  }});
  if (i == 0)
        window.clearInterval(timer);
}, 1000);   // Repeat every 1 second

Please note that I have used JQuery for the ajax call

The other way might be to use a StreamingHttpResponse

In this case your loop should be separated into a method and the returned value should be sent to the html page or as the response

def count_seconds():
    for i in range (10,0,-1):
        time.sleep(1)
        yield 'Wait for %d seconds \r' % i,

The yield keyword will return the ith value every time the function is called.

Upvotes: 1

Related Questions