Skip to content
JRFASTER edited this page May 5, 2021 · 5 revisions

Flask is the Python library used for the backend API. The Flask application is created in app.py. app.py creates the Flask app, sets the configuration values, and registers the Blueprints

Blueprints

Blueprints are a Flask feature that allow the API to extend across Python files. If we didn't use Blueprints, then all Python code for API methods would need to be in the app.py file. Blueprints allow for better organization of the API code. For example, the API methods that pertain to authentication can be placed in the auth.py file using Blueprints.

Creating a Blueprint

  1. Create a new Python file
  2. Add the following imports (only the first import is actually required for a Blueprint)
from flask import Blueprint
from flask import request
from flask import make_response
from http import HTTPStatus
  1. Create the Blueprint with the following Python code: name_api = Blueprint('name_api', __name__). Make sure to replace name in name_api with the name of the file (without the .py). For example, the auth.py file defines the Blueprint like auth_api = Blueprint('auth_api', __name__)
  2. Register the Blueprint in app.py with the following Python code: from fileName import name_api
  • Add the import in app.py with the following Python code: from fileName import name_api. Replace fileName with the name of the file (without the .py) and replace name in name_api with the name from Step 3.
  • Add the line of code to register the Blueprint with Flask: app.register_blueprint(name_api, url_prefix='/prefix'). Replace name in name_api with the name of the file (without the .py) from Step 3. The url_prefix is optional. The url_prefix will be required before the methods in the Blueprint. For example, if auth.py has a method called login and has the prefix of "/auth", then the URL must be http://localhost:5000/auth/login. If no prefix is specified, then the URL will be http://localhost:5000/login.

Adding an API method to a blueprint

  1. Create a method for the API
  2. Add an annotation to the method like the following: @name_api.route('/url', methods = ['POST']). Replace name_api with the name of the Blueprint (See Step 3 in Creating a Blueprint. Replace '/url' with the path for the API method. Update methods with the desired HTTP methods the API call will take.

Flask CORS

Cross-origin resource sharing (CORS) is a security HTTP Header that restricts who can access resources for a given domain. Without including this header, browsers will prevent example.com from accessing resources from service.example.com. For the API, we want any domain to be able to call the API. Thus, the CORS HTTP Header will need to be added to every response letting the browser know its ok for any domain to call our API. The flask_cors Python library does just that. It adds the appropriate HTTP CORS header to every response to allow any domain to call the API. A call to CORS(app) in app.py sets up the CORS header for every request.

Flask Doc

Here is a link to a flask document that is still in progress. I making notes here is the link to the doc: https://docs.google.com/document/d/18_ONEKNqEBTvAebX3rGSeVx6X-EXiUpmYvFvinwNRx0/edit?usp=sharing

Clone this wiki locally