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

Pytest: Flask echo GET

A simple web application with a static page showing an HTML form with a text box and a button. Pressing the button will send the text we enetered in the box to the server which will echo it back.

We need to install flask using pip install flask then we can run the application:

flask --app echo_get --debug run

Then visit http://localhost:5000/ and try the “application”.

Stop the web server by clicking “Ctrl-C”.

from flask import Flask, request

app = Flask(__name__)

@app.route("/")
def hello():
    return '''
<form action="/echo" method="GET">
<input name="text">
<input type="submit" value="Echo">
</form>
'''

@app.route("/echo")
def echo():
    answer = request.args.get('text')
    if answer:
        return "You said: " + answer
    else:
        return "Nothing to say?"