flask cors

  1. Import the necessary modules in your Flask application:
from flask import Flask
from flask_cors import CORS
  1. Create a Flask application instance:
app = Flask(__name__)
  1. Initialize CORS extension with the Flask app:
CORS(app)
  1. Optionally, configure CORS for specific routes or globally:

a. To apply CORS globally for all routes:

python cors = CORS(app, resources={r"/": {"origins": ""}})

b. To apply CORS for specific routes:

python @app.route("/your_route") @cross_origin(origin="*", methods=["GET", "POST"]) def your_route_function(): # Your route logic here

Replace "/your_route" with the actual route you want to apply CORS to.

  1. Run your Flask application:
if __name__ == "__main__":
    app.run(debug=True)

Ensure that you have the necessary dependencies installed, including Flask and Flask-CORS. You can install them using:

pip install Flask Flask-CORS

This setup enables Cross-Origin Resource Sharing (CORS) in your Flask application, allowing it to handle requests from different origins. Adjust the CORS settings based on your specific requirements.