Categories
Python Answers

How to enable CORS in Python Flask?

Spread the love

Sometimes, we want to enable CORS in Python Flask.

In this article, we’ll look at how to enable CORS in Python Flask.

How to enable CORS in Python Flask?

To enable CORS in Python Flask, we can use the CORS class.

For instance, w write

from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route("/")
@cross_origin()
def helloWorld():
  return "Hello"

to create an app with CORS enabled.

We enable CORS on the app with cors = CORS(app).

And then we enable CORS our view function with the @cross_origin() decorator.

Conclusion

To enable CORS in Python Flask, we can use the CORS class.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *