Menu Close

How to enable CORS in Python Flask?

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.

Posted in Python, Python Answers