Skip to main content

Section 14.8 Web Applications With a User Interface

This section builds on the material in the preceding sections to present a web application that prompts the user to provide input, performs some processing, and displays results.

from flask import Flask, request

app = Flask(__name__)

def home():
    return HOME_HTML

        <h2>Welcome to the Greeter</h2>
        <form action="/greet">
            What's your name? <input type='text' name='username'><br>
            What's your favorite food? <input type='text' name='favfood'><br>
            <input type='submit' value='Continue'>

def greet():
    username = request.args.get('username', '')
    favfood = request.args.get('favfood', '')
    if username == '':
        username = 'World'
    if favfood == '':
        msg = 'You did not tell me your favorite food.'
        msg = 'I like ' + favfood + ', too.'

    return GREET_HTML.format(username, msg)

        <h2>Hello, {0}!</h2>

if __name__ == "__main__":
    # Launch the Flask dev server"localhost", debug=True)

The program is organized as follows:

  • Lines 6-8 define the home() function, which defines the starting page for the application. It displays a form that prompts for the user's name and favorite food.

  • The form's action attribute on Line 13 specifies that the form submission will be directed to the path /greet. Processing for this path is defined by the greet() function on lines 20-31.

  • Lines 22-29 extract the information submitted on the form and compute a response message.

You have attempted of activities on this page.