Building REST APIs: Flask
00:00 Building REST APIs: Flask In this course, you’ll look at three popular frameworks for building REST APIs in Python. Each framework has pros and cons, so you’ll need to evaluate which works best for you.
00:14
To this end, you’ll look at a REST API in each framework and all the examples will be for a similar API that manages a collection of countries. Each country will have these fields: name
is the name of the country, capital
is the capital of the country, and area
is the area of the country in square kilometers. The fields name
, capital
, and area
store data about a specific country somewhere in the world.
00:39 Most of the time, data sent from a REST API comes from a database. Connecting to a database is beyond the scope of this course, so for two of these examples, you’ll store your data in a Python list.
00:50 The exception to this is the Django REST Framework, which runs off the SQLite database that Django creates.
00:57 It’s advised that you create individual folders for each of the examples to separate the source files. You’ll also want to use virtual environments to isolate any dependencies.
01:06
To keep things consistent, you’ll use /countries
as your main endpoint for all three frameworks. You’ll also use JSON as your data format. So now you’ve got the background for the API, let’s look at the first framework: Flask.
01:22 Flask is a Python microframework used to build web applications and REST APIs. It provides a solid backbone for your applications while leaving many design choices up to you. Its main job is to handle HTTP requests and route them to the appropriate function in the application.
01:38 First, you’ll need to install Flask and it’s best to do this and the other frameworks in a virtual environment. You’ll have created one earlier in the course when using requests, so make sure it’s active and then install Flask to it.
01:51 The first line will activate the virtual environment on macOS or Linux, while the second line installs Flask.
02:08 And here’s what you need to do the same on Windows.
02:23
Once Flask is installed, create your code in a file named flask_app.py
. This application defines the API endpoint /countries
to manage the list of countries.
02:34
It handles two kinds of requests: GET /countries
returns, the list of countries and POST /countries
adds a new country to the list.
02:43 In a full REST API, you’d want to expand this to include functions for all the required operations.
03:29
This helper function uses a generator expression to select all the country IDs and then calls max()
on them to get the largest value. It increments this value by one to get the next ID to use.
03:40
This will be used later on in the code to allow the creation of new countries. The function for the GET
request to /countries
comes first.
03:48
It uses @app.get
, which is a Flask route decorator to connect, GET
requests to a function in the application. When you access /countries
, Flask calls the decorated function to handle the HTTP request and return a response. get_countries()
takes /countries
, which is a Python list, and converts it to JSON with jsonify()
.
04:09 This JSON is returned in the response, which you’ll see later.
04:14
The next function is to handle the POST
request and uses Flask’s @app.post
decorator to link the POST
request to /countries
to this function. Inside add_country()
, the first check is that the request is properly formatted JSON.
04:28
If it is, then the country data is extracted from the JSON using the get_json()
method. The ID for the new country is found using the next_id()
helper function, and then countries
is appended to the list of dictionaries, so it can be handled in the same way as any other country.
04:44
The country is then returned along with the status code of 201
indicating that creation was successful.
04:52
If the check for JSON failed, then the return value is the error message, along with a 415
status code indicating an unsupported media type as only JSON is accepted by this API.
05:06
Flask can be run directly from the terminal with a single line. You need to pass in the --app
option with the name of your app minus the .py
extension, and also any other options you want.
05:17 In this particular case, it’s a good idea to run in debug mode. The main advantage for you here is that any changes you make to your app’s code in the editor will lead to the app being reloaded instantly reflecting the current state of your script.
05:30 This can save a lot of time and head scratching if you’d forgotten to reload the server and couldn’t understand why the changes you’ve made weren’t being reflected.
05:39 I know this from bitter experience.
05:44 This starts up a server running the application.
05:49 Open up your browser and go to the address seen on screen, and you should see the response. This JSON response contains the three countries defined in your Flask app.
06:02
Note that back in the terminal window, you’ll see Flask outputting logging of the requests it’s received and how it’s dealt with them. You can safely ignore any lines referring to favicons, generating a 404
error.
06:16 Now you have the Flask app up and running, in the next section of the course, you’ll take a look at adding an extra endpoint and sending more requests to the app.
Become a Member to join the conversation.