Flask-Cors


NameFlask-Cors JSON
Version 4.0.1 PyPI version JSON
download
home_pagehttps://github.com/corydolphin/flask-cors
SummaryA Flask extension adding a decorator for CORS support
upload_time2024-05-04 19:49:43
maintainerNone
docs_urlNone
authorCory Dolphin
requires_pythonNone
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Flask-CORS
==========

|Build Status| |Latest Version| |Supported Python versions|
|License|

A Flask extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible.

This package has a simple philosophy: when you want to enable CORS, you wish to enable it for all use cases on a domain.
This means no mucking around with different allowed headers, methods, etc.

By default, submission of cookies across domains is disabled due to the security implications.
Please see the documentation for how to enable credential'ed requests, and please make sure you add some sort of `CSRF <http://en.wikipedia.org/wiki/Cross-site_request_forgery>`__ protection before doing so!

Installation
------------

Install the extension with using pip, or easy\_install.

.. code:: bash

    $ pip install -U flask-cors

Usage
-----

This package exposes a Flask extension which by default enables CORS support on all routes, for all origins and methods.
It allows parameterization of all CORS headers on a per-resource level.
The package also contains a decorator, for those who prefer this approach.

Simple Usage
~~~~~~~~~~~~

In the simplest case, initialize the Flask-Cors extension with default arguments in order to allow CORS for all domains on all routes.
See the full list of options in the `documentation <https://flask-cors.corydolphin.com/en/latest/api.html#extension>`__.

.. code:: python


    from flask import Flask
    from flask_cors import CORS

    app = Flask(__name__)
    CORS(app)

    @app.route("/")
    def helloWorld():
      return "Hello, cross-origin-world!"

Resource specific CORS
^^^^^^^^^^^^^^^^^^^^^^

Alternatively, you can specify CORS options on a resource and origin level of granularity by passing a dictionary as the `resources` option, mapping paths to a set of options.
See the full list of options in the `documentation <https://flask-cors.corydolphin.com/en/latest/api.html#extension>`__.

.. code:: python

    app = Flask(__name__)
    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

    @app.route("/api/v1/users")
    def list_users():
      return "user example"

Route specific CORS via decorator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This extension also exposes a simple decorator to decorate flask routes with.
Simply add ``@cross_origin()`` below a call to Flask's ``@app.route(..)`` to allow CORS on a given route.
See the full list of options in the `decorator documentation <https://flask-cors.corydolphin.com/en/latest/api.html#decorator>`__.

.. code:: python

    @app.route("/")
    @cross_origin()
    def helloWorld():
      return "Hello, cross-origin-world!"

Documentation
-------------

For a full list of options, please see the full `documentation <https://flask-cors.corydolphin.com/en/latest/api.html>`__

Troubleshooting
---------------

If things aren't working as you expect, enable logging to help understand what is going on under the hood, and why.

.. code:: python

    logging.getLogger('flask_cors').level = logging.DEBUG


Tests
-----

A simple set of tests is included in ``test/``.
To run, install nose, and simply invoke ``nosetests`` or ``python setup.py test`` to exercise the tests.

If nosetests does not work for you, due to it no longer working with newer python versions.
You can use pytest to run the tests instead.

Contributing
------------

Questions, comments or improvements?
Please create an issue on `Github <https://github.com/corydolphin/flask-cors>`__, tweet at `@corydolphin <https://twitter.com/corydolphin>`__ or send me an email.
I do my best to include every contribution proposed in any way that I can.

Credits
-------

This Flask extension is based upon the `Decorator for the HTTP Access Control <https://web.archive.org/web/20190128010149/http://flask.pocoo.org/snippets/56/>`__ written by Armin Ronacher.

.. |Build Status| image:: https://github.com/corydolphin/flask-cors/actions/workflows/unittests.yaml/badge.svg
   :target: https://travis-ci.org/corydolphin/flask-cors
.. |Latest Version| image:: https://img.shields.io/pypi/v/Flask-Cors.svg
   :target: https://pypi.python.org/pypi/Flask-Cors/
.. |Supported Python versions| image:: https://img.shields.io/pypi/pyversions/Flask-Cors.svg
   :target: https://img.shields.io/pypi/pyversions/Flask-Cors.svg
.. |License| image:: http://img.shields.io/:license-mit-blue.svg
   :target: https://pypi.python.org/pypi/Flask-Cors/

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/corydolphin/flask-cors",
    "name": "Flask-Cors",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Cory Dolphin",
    "author_email": "corydolphin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/40/6a/a8d56d60bcfa1ec3e4fdad81b45aafd508c3bd5c244a16526fa29139d7d4/flask_cors-4.0.1.tar.gz",
    "platform": "any",
    "description": "Flask-CORS\n==========\n\n|Build Status| |Latest Version| |Supported Python versions|\n|License|\n\nA Flask extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible.\n\nThis package has a simple philosophy: when you want to enable CORS, you wish to enable it for all use cases on a domain.\nThis means no mucking around with different allowed headers, methods, etc.\n\nBy default, submission of cookies across domains is disabled due to the security implications.\nPlease see the documentation for how to enable credential'ed requests, and please make sure you add some sort of `CSRF <http://en.wikipedia.org/wiki/Cross-site_request_forgery>`__ protection before doing so!\n\nInstallation\n------------\n\nInstall the extension with using pip, or easy\\_install.\n\n.. code:: bash\n\n    $ pip install -U flask-cors\n\nUsage\n-----\n\nThis package exposes a Flask extension which by default enables CORS support on all routes, for all origins and methods.\nIt allows parameterization of all CORS headers on a per-resource level.\nThe package also contains a decorator, for those who prefer this approach.\n\nSimple Usage\n~~~~~~~~~~~~\n\nIn the simplest case, initialize the Flask-Cors extension with default arguments in order to allow CORS for all domains on all routes.\nSee the full list of options in the `documentation <https://flask-cors.corydolphin.com/en/latest/api.html#extension>`__.\n\n.. code:: python\n\n\n    from flask import Flask\n    from flask_cors import CORS\n\n    app = Flask(__name__)\n    CORS(app)\n\n    @app.route(\"/\")\n    def helloWorld():\n      return \"Hello, cross-origin-world!\"\n\nResource specific CORS\n^^^^^^^^^^^^^^^^^^^^^^\n\nAlternatively, you can specify CORS options on a resource and origin level of granularity by passing a dictionary as the `resources` option, mapping paths to a set of options.\nSee the full list of options in the `documentation <https://flask-cors.corydolphin.com/en/latest/api.html#extension>`__.\n\n.. code:: python\n\n    app = Flask(__name__)\n    cors = CORS(app, resources={r\"/api/*\": {\"origins\": \"*\"}})\n\n    @app.route(\"/api/v1/users\")\n    def list_users():\n      return \"user example\"\n\nRoute specific CORS via decorator\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThis extension also exposes a simple decorator to decorate flask routes with.\nSimply add ``@cross_origin()`` below a call to Flask's ``@app.route(..)`` to allow CORS on a given route.\nSee the full list of options in the `decorator documentation <https://flask-cors.corydolphin.com/en/latest/api.html#decorator>`__.\n\n.. code:: python\n\n    @app.route(\"/\")\n    @cross_origin()\n    def helloWorld():\n      return \"Hello, cross-origin-world!\"\n\nDocumentation\n-------------\n\nFor a full list of options, please see the full `documentation <https://flask-cors.corydolphin.com/en/latest/api.html>`__\n\nTroubleshooting\n---------------\n\nIf things aren't working as you expect, enable logging to help understand what is going on under the hood, and why.\n\n.. code:: python\n\n    logging.getLogger('flask_cors').level = logging.DEBUG\n\n\nTests\n-----\n\nA simple set of tests is included in ``test/``.\nTo run, install nose, and simply invoke ``nosetests`` or ``python setup.py test`` to exercise the tests.\n\nIf nosetests does not work for you, due to it no longer working with newer python versions.\nYou can use pytest to run the tests instead.\n\nContributing\n------------\n\nQuestions, comments or improvements?\nPlease create an issue on `Github <https://github.com/corydolphin/flask-cors>`__, tweet at `@corydolphin <https://twitter.com/corydolphin>`__ or send me an email.\nI do my best to include every contribution proposed in any way that I can.\n\nCredits\n-------\n\nThis Flask extension is based upon the `Decorator for the HTTP Access Control <https://web.archive.org/web/20190128010149/http://flask.pocoo.org/snippets/56/>`__ written by Armin Ronacher.\n\n.. |Build Status| image:: https://github.com/corydolphin/flask-cors/actions/workflows/unittests.yaml/badge.svg\n   :target: https://travis-ci.org/corydolphin/flask-cors\n.. |Latest Version| image:: https://img.shields.io/pypi/v/Flask-Cors.svg\n   :target: https://pypi.python.org/pypi/Flask-Cors/\n.. |Supported Python versions| image:: https://img.shields.io/pypi/pyversions/Flask-Cors.svg\n   :target: https://img.shields.io/pypi/pyversions/Flask-Cors.svg\n.. |License| image:: http://img.shields.io/:license-mit-blue.svg\n   :target: https://pypi.python.org/pypi/Flask-Cors/\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Flask extension adding a decorator for CORS support",
    "version": "4.0.1",
    "project_urls": {
        "Homepage": "https://github.com/corydolphin/flask-cors"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8b522aa6285f104616f73ee1ad7905a16b2b35af0143034ad0cf7b64bcba715c",
                "md5": "eab82d61c7dd02e7b05ec864bd411f33",
                "sha256": "f2a704e4458665580c074b714c4627dd5a306b333deb9074d0b1794dfa2fb677"
            },
            "downloads": -1,
            "filename": "Flask_Cors-4.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "eab82d61c7dd02e7b05ec864bd411f33",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 14290,
            "upload_time": "2024-05-04T19:49:41",
            "upload_time_iso_8601": "2024-05-04T19:49:41.721787Z",
            "url": "https://files.pythonhosted.org/packages/8b/52/2aa6285f104616f73ee1ad7905a16b2b35af0143034ad0cf7b64bcba715c/Flask_Cors-4.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "406aa8d56d60bcfa1ec3e4fdad81b45aafd508c3bd5c244a16526fa29139d7d4",
                "md5": "1e1eac64500b970dcb2112f0c6989664",
                "sha256": "eeb69b342142fdbf4766ad99357a7f3876a2ceb77689dc10ff912aac06c389e4"
            },
            "downloads": -1,
            "filename": "flask_cors-4.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1e1eac64500b970dcb2112f0c6989664",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 30306,
            "upload_time": "2024-05-04T19:49:43",
            "upload_time_iso_8601": "2024-05-04T19:49:43.538224Z",
            "url": "https://files.pythonhosted.org/packages/40/6a/a8d56d60bcfa1ec3e4fdad81b45aafd508c3bd5c244a16526fa29139d7d4/flask_cors-4.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-04 19:49:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "corydolphin",
    "github_project": "flask-cors",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "flask-cors"
}
        
Elapsed time: 0.29577s