简体   繁体   中英

using for loops with bottle

I am currently developing a python web-app based on bottle so what I am trying to do is print the outcome of a for loop I have tried the following

#!/usr/bin/python
from bottle import *
@route('/')
def index():
    for i in range(10):
         return i

but this did not work and i got this from the development server output

localhost - - [13/Jan/2017 18:11:38] "GET /request HTTP/1.1" 200 0
localhost - - [13/Jan/2017 18:11:40] "GET /favicon.ico HTTP/1.1" 200 0

so i tried this

#!/usr/bin/python
from bottle import *
@route('/')
def index():
    sumOfValues=0
    for i in range(10):
        sumOfValues+=i
    return sumOfValues

this also did not work and my devlopment server gave me this

localhost - - [13/Jan/2017 18:15:44] "GET /request HTTP/1.1" 500 746
localhost - - [13/Jan/2017 18:15:46] "GET /favicon.ico HTTP/1.1" 500 750

so how can I do it I tried searching google but nothing came back , thanks in advance

If you return in a function, it immediately ends.

It looks like you want to do a streaming response of-sorts. Bottle can do this, but you must yield items .

See also:

函数必须返回字符串 - 所以使用return str(sumOfValues)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM