### Set Up Local Development Environment for Flask-CORS Source: https://github.com/corydolphin/flask-cors/blob/main/CONTRIBUTING.md This snippet outlines the initial steps to set up your local development environment for the `flask-cors` project, including cloning the repository, navigating into the directory, installing dependencies with `uv`, and setting up pre-commit hooks for code quality. ```bash cd git clone git@github.com:YOUR_NAME/flask-cors.git ``` ```bash cd flask-cors ``` ```bash uv sync ``` ```bash uv run pre-commit install ``` -------------------------------- ### Install Flask-CORS using pip Source: https://github.com/corydolphin/flask-cors/blob/main/README.rst This snippet demonstrates how to install the Flask-CORS extension using the pip package manager. The `-U` flag ensures that the package is upgraded if it's already installed, providing the latest version. ```bash $ pip install -U flask-cors ``` -------------------------------- ### Install Flask-CORS via pip Source: https://github.com/corydolphin/flask-cors/blob/main/docs/index.md This snippet provides the command to install the Flask-CORS extension using pip, a common Python package manager. The `-U` flag ensures that the package is upgraded if already installed. ```console pip install -U flask-cors ``` -------------------------------- ### Enable CORS for all routes with Flask-CORS Source: https://github.com/corydolphin/flask-cors/blob/main/README.rst This example shows the simplest way to initialize Flask-CORS. By calling `CORS(app)`, all routes in the Flask application will have CORS enabled by default for all origins and methods. This allows cross-origin AJAX requests to any endpoint without further configuration. ```python from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route("/") def helloWorld(): return "Hello, cross-origin-world!" ``` -------------------------------- ### Enable CORS with cookie support in Flask-CORS Source: https://github.com/corydolphin/flask-cors/blob/main/docs/index.md This Python example shows how to configure Flask-CORS to support credentialed requests, such as those involving cookies. By setting `supports_credentials=True` during `CORS` initialization, cross-origin requests can include credentials, though this requires careful consideration of CSRF protection. ```python from flask import Flask, session from flask_cors import CORS app = Flask(__name__) CORS(app, supports_credentials=True) @app.route("/") def helloWorld(): return "Hello, %s" % session['username'] ``` -------------------------------- ### Enable CORS for a single route using decorator Source: https://github.com/corydolphin/flask-cors/blob/main/README.rst This example illustrates how to enable CORS for an individual Flask route using the `@cross_origin()` decorator. Placing this decorator below `@app.route()` applies CORS settings specifically to that endpoint, offering fine-grained control over cross-origin access without affecting other routes. ```python @app.route("/") @cross_origin() def helloWorld(): return "Hello, cross-origin-world!" ``` -------------------------------- ### Initialize Flask-CORS for all routes and origins Source: https://github.com/corydolphin/flask-cors/blob/main/docs/index.md This Python snippet demonstrates the basic initialization of Flask-CORS. It imports `Flask` and `CORS`, then creates a Flask application instance and applies `CORS(app)` to enable Cross-Origin Resource Sharing for all routes, origins, and methods by default. ```python from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route("/") def helloWorld(): return "Hello, cross-origin-world!" ``` -------------------------------- ### Manage Local Development Branches and Run Tests Source: https://github.com/corydolphin/flask-cors/blob/main/CONTRIBUTING.md This snippet provides commands for creating a new development branch, running formatting checks, executing unit tests, and performing cross-Python version tests using `tox` before submitting a pull request to ensure code quality and compatibility. ```bash git checkout -b name-of-your-bugfix-or-feature ``` ```bash make check ``` ```bash make test ``` ```bash tox ``` -------------------------------- ### Commit and Push Changes to GitHub Source: https://github.com/corydolphin/flask-cors/blob/main/CONTRIBUTING.md This snippet details the standard Git workflow for staging, committing, and pushing your local changes to your forked repository on GitHub, preparing them for a pull request. ```bash git add . git commit -m "Your detailed description of your changes." git push origin name-of-your-bugfix-or-feature ``` -------------------------------- ### Configure Flask-CORS for specific resources Source: https://github.com/corydolphin/flask-cors/blob/main/README.rst This snippet demonstrates how to apply CORS settings to specific resources or URL patterns. By passing a dictionary to the `resources` option of `CORS(app)`, you can define granular CORS policies, such as allowing all origins for paths under `/api/*` while leaving other routes unaffected. ```python app = Flask(__name__) cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) @app.route("/api/v1/users") def list_users(): return "user example" ``` -------------------------------- ### Enable debug logging for Flask-CORS Source: https://github.com/corydolphin/flask-cors/blob/main/README.rst This snippet shows how to enable debug logging for the Flask-CORS extension. Setting the logger level to `logging.DEBUG` provides detailed output, which is useful for troubleshooting and understanding the extension's behavior during development, helping to diagnose CORS-related issues. ```python logging.getLogger('flask_cors').level = logging.DEBUG ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.