Open In App

How to Install Flask-CORS in Python

Last Updated : 01 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to install Flask-CORS which is a Python module and a Flask extension that supports and allows cross-origin resource sharing (CORS) through a simple decorator. Due to security concerns, cross-domain cookie submission is disabled by default. We will look at how to install Flask-Cors on the Linux operating system.

What is Python CORS?

When one domain requests a resource from another domain, this is referred to as cross-origin resource sharing or CORS. When a front-end and back-end are in distinct origins, this happens most commonly. Using JavaScript scripts, which share similar principles with the backend, the frontend communicates with the backend. Cross-origin AJAX is made possible with Flask CORS, which is only the Flask plugin for managing CORS.

What are Flask-Cors used for?

  1. It enables communication with resources housed in many domains.
  2. It’s a crucial protocol for enabling cross-domain queries when there is a justifiable need to do so.

Install the Flask-Cors package

Step 1: Run the below command in the terminal to install Python3 on your Linux OS:

sudo apt-get install python3
How to Enable CORS in the Flask

 

Step 2: Next, install the pip module, which is required for Python3 package installation and management. As a result, use the below command:

sudo apt install python3-pip
How to Enable CORS in the Flask

 

Step 3: Once the installation is complete, verify it by looking at the Python and pip versions:

python3 --version
pip --version 

The version number may differ, but it will look like this:

Install the Flask-Cors package

 

Step 4: Use the below command to install the Flask-Cors package:

pip install Flask-Cors
Install the Flask-Cors package

 

Step 5: Run the below command in the terminal to see if the Flask-Cors package has been successfully installed on your Linux Machine:

python3 -m pip show Flask-Cors

If the installation is successful, the following information would be visible on the terminal screen:

Install the Flask-Cors package

 

How to Enable CORS in the Flask

Method 1: To enable CORS for all domains on all routes, initialize the Flask-Cors extension with default parameters.

Python3




from flask import Flask
from flask_cors import CORS
  
app = Flask(__name__)
CORS(app)
  
  
@app.route("/")
def welcome():
    return "Hello world! Welcome by Kushal"


Method 2: Simply add @cross_origin() below a call route to Flask’s @app. route(..) to allow CORS on a given route.

Python3




@app.route("/")
@cross_origin()
def helloWorld():
    return "Hello world! Welcome by Kushal"


Usage of Flask-Cors Package

This package exposes a Flask extension that enables CORS support by default on all routes, origins, and methods and provides a simple decorator for decorating Flask routes. To accept the default options and allow CORS on a given route, @cross origin() is to be added after a call to Flask’s @app.route(). Refer to the following function with different options to learn further about it.

Python




flask_cors.cross_origin(
origins = '*'
methods = ['GET', 'HEAD', 'POST', 'OPTIONS', 'PUT'], 
headers = None
supports_credentials = False
max_age = None
send_wildcard = True
always_send = True
automatic_options = False
)




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads