Getting started with the Flask framework

Presentation

To speed up and ease development, most web servers are developed using a framework. Flask is a popular micro web framework, in that it provides the minimum set of technologies to help you build server-side web applications. It is not as feature-full as some of its competitors—such as Django, but it is small, lightweight, and easy to use. In this activity we develop the same service as in Activity 6, but using Flask. In addition to providing an example of use of a framework, using Flask will allow us to deploy our server on the PythonAnywhere cloud to easily make it accessible from the Internet.

Check to see if Flask is installed (pip show flask) and install it if necessary (pip install flask).

Hello World

Write the minimal version of the server:

Run the program.
Flask confirms it is up and running and waiting to service web requests at Flask’s test web address (127.0.0.1) and protocol port number (5000):

Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
If you see this message, all is well. Flask’s web server is ready and waiting.
Open your browser and enter http://localhost:5000/ in the address bar.
The “Hello World!” message should appear in your browser’s window. In addition to this, take a look at the terminal window where your webapp is running. A new status message should’ve appeared too:
127.0.0.1 - - [12/Jan/2024 07:59:20] "GET / HTTP/1.1" 200 -

/members

Using the same principle as the previous path, implement the /members path. Test using a web browser or curl.

/members/license

This path demonstrates the use of a variable in a Flask route, and how to return a status code different from the default 200 OK:

/membersInRange?minAge=X&maxAge=Y

This last path demonstrates the usage of url arguments: