kata
kata

Reputation: 87

Using bottle.py to print on browser

I am new to python and especially in using modules.I have to use bottle.py module . Is there any possible way to print something in my browser , without having to return it ? To be more specific , I want something like this:

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)

How can I print something like "Trying to connect to database without having to return it ?

Upvotes: 0

Views: 1235

Answers (1)

notorious.no
notorious.no

Reputation: 5107

The print syntax/function will only display on stdout not on the browser. Use yield instead of return to "gradually display content" (for lack of better words). I used to favor Bottle over Flask for this very reason (Flask has a different way of doing it though).

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)

Upvotes: 4

Related Questions