Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Flask Debugging

If you launch the Flask application using the --debug flag and your application enconteres and exception, you can get the browser to open an interactive prompt where you can execute arbitrary Python code.

from flask import Flask, request
import mymath

app = Flask(__name__)

@app.route("/")
def main():
    return '''
    <form action="/area" method="GET">
         Width: <input name="width">
         Height: <input name="height">
         <input type="submit" value="Area">
     </form>
'''

@app.get("/area")
def area():
    width = request.args.get('width')
    height = request.args.get('height')
    if width is None:
        return 'Missing width'
    if height is None:
        return 'Missing height'
    return "The area is {}".format(mymath.area(int(width), int(height)))

$ flask --app app --debug run
import app

def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'Width' in rv.data

    # TODO: add more tests