使用 bottle.py 在浏览器上打印
Using bottle.py to print on browser
我是 python 的新手,尤其是在使用 modules.I 时必须使用 bottle.py 模块。有什么可能的方法可以在我的浏览器中打印一些东西,而不必 return 它吗?更具体地说,我想要这样的东西:
import pymysql
from bottle import Bottle,run
app = Bottle()
@app.route('/conn')
def conn():
**print("Trying to connect to database...")**
try:
conn = pymysql.connect(user="X",passwd="X",host="X",port=X,database="X")
return "Connection succeded"
except:
return "Oops...connection failed"
run(app, host='localhost',port = 8080)
如何打印类似“尝试连接到数据库而无需 return 之类的内容?
print
syntax/function 只会显示在标准输出上,不会显示在浏览器上。使用 yield
而不是 return
到 "gradually display content"(因为缺少更好的词)。正是出于这个原因,我过去更喜欢 Bottle 而不是 Flask(尽管 Flask 有不同的方式)。
import pymysql
from bottle import Bottle,run
app = Bottle()
@app.route('/conn')
def conn():
yield "Trying to connect to database..."
try:
conn = pymysql.connect(user="X",passwd="X",host="X",port=X,database="X")
yield "Connection succeded"
except:
yield "Oops...connection failed"
run(app, host='localhost',port = 8080)
我是 python 的新手,尤其是在使用 modules.I 时必须使用 bottle.py 模块。有什么可能的方法可以在我的浏览器中打印一些东西,而不必 return 它吗?更具体地说,我想要这样的东西:
import pymysql
from bottle import Bottle,run
app = Bottle()
@app.route('/conn')
def conn():
**print("Trying to connect to database...")**
try:
conn = pymysql.connect(user="X",passwd="X",host="X",port=X,database="X")
return "Connection succeded"
except:
return "Oops...connection failed"
run(app, host='localhost',port = 8080)
如何打印类似“尝试连接到数据库而无需 return 之类的内容?
print
syntax/function 只会显示在标准输出上,不会显示在浏览器上。使用 yield
而不是 return
到 "gradually display content"(因为缺少更好的词)。正是出于这个原因,我过去更喜欢 Bottle 而不是 Flask(尽管 Flask 有不同的方式)。
import pymysql
from bottle import Bottle,run
app = Bottle()
@app.route('/conn')
def conn():
yield "Trying to connect to database..."
try:
conn = pymysql.connect(user="X",passwd="X",host="X",port=X,database="X")
yield "Connection succeded"
except:
yield "Oops...connection failed"
run(app, host='localhost',port = 8080)