0

Assume if I want to do a simple loop and I will take some time to execute it.

for i in range(0, 100000000000):
    print i

Now, here is the code for Bottle which I have. I want to print out all the intermediate output like 1,2,3,4... before the loop ends.

import bottle

@bottle.route('/')
def home_page():
    total = 0
    for i in range(0, 100000000000):
        total = i + 0
        print i
    return bottle.template('hello.tpl', {"total": total})

How can I output i during iteration? I tried "response" but it didn't work, it still have to return anyway.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 2
    I think you've misunderstood the fundamentals of how web pages work.. You could probably get something working with polling your backend with an ajax request but for the most part its not something thats done often. Also, from what I can tell "flask" has nothing to do with your question – Sayse Jan 08 '16 at 08:38
  • 1
    But thats just it, web pages work by sending a request and waiting for a *single* response. So you could store the progress somewhere and keep sending requests for that progress but that is too broad for an answer on stackoverflow. – Sayse Jan 08 '16 at 08:42
  • It's actually possible to push partial content to the browser through http. PHP implements a feature called flush and if I remember correctly it automatically flushes content after some given size/time threshold which may lead a beginner to think that partial responses is a rule rather than exception. Look into this question as an example: http://stackoverflow.com/questions/6300859/is-there-a-function-in-django-python-similar-to-php-flush-that-lets-me-send – Alexander Holmbäck Jan 08 '16 at 09:15

2 Answers2

0

You cannot output i to html during iteration. What you can do is to return list of items to your template and render them before return HTTP response as @Baterson suggested.

If you need to debug your application consider using python debugger. Also, setup python logging on your project could simplify error tracking over your project.

In code snippet above print would display data in your console (in which your have run web server, so if you need to check what is value if i switch to console and see what is going on there).

Small suggestion to you is to get familiar with MVC Design Paradigm, after reading and understanding that article it would be quite easier for you to work with botle.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
0

I think you're asking about a long poll. The Bottle docs mention this in the context of using Greenlets. Here's the example from there:

from gevent import monkey; monkey.patch_all()

from time import sleep
from bottle import route, run

@route('/stream')
def stream():
    yield 'START'
    sleep(3)
    yield 'MIDDLE'
    sleep(5)
    yield 'END'

run(host='0.0.0.0', port=8080, server='gevent')

There are also some other SO questions that discuss this.

Good luck!

Community
  • 1
  • 1
ron rothman
  • 17,348
  • 7
  • 41
  • 43