# 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.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  from flask import Flask, request from datetime import datetime app = Flask(__name__) @app.route('/') def home(): return """

Welcome to the Greeter

""" @app.route('/greet') def greet(): username = request.args.get('username', 'World') favfood = request.args['favfood'] if favfood == '': msg = 'You did not tell me your favorite food.' else: msg = 'I like ' + favfood + ', too.' return """

Hello, {0}!

{1} """.format(username, msg) # Launch the FlaskPy dev server app.run(host="localhost", debug=True) 

The program is organized as follows:

• Lines 6-17 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 9 specifies that the form submission will be directed to the path /greet. Processing for this path is defined by the greet() function on lines 19-33.

• Lines 21-26 extract the information submitted on the form and compute a response message.