{"id":13458959,"url":"https://github.com/python-injector/flask_injector","last_synced_at":"2025-12-12T00:40:51.687Z","repository":{"id":5550604,"uuid":"6754801","full_name":"python-injector/flask_injector","owner":"python-injector","description":"Adds Injector support to Flask.","archived":false,"fork":false,"pushed_at":"2024-05-08T17:18:49.000Z","size":200,"stargazers_count":284,"open_issues_count":16,"forks_count":42,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-30T00:08:07.943Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/python-injector.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-11-19T04:09:14.000Z","updated_at":"2025-03-18T22:05:27.000Z","dependencies_parsed_at":"2024-06-18T16:49:06.221Z","dependency_job_id":null,"html_url":"https://github.com/python-injector/flask_injector","commit_stats":{"total_commits":187,"total_committers":12,"mean_commits":"15.583333333333334","dds":0.1657754010695187,"last_synced_commit":"993c0bbe5063bef511da60d8b25383a498a59f09"},"previous_names":["alecthomas/flask_injector"],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-injector%2Fflask_injector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-injector%2Fflask_injector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-injector%2Fflask_injector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/python-injector%2Fflask_injector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/python-injector","download_url":"https://codeload.github.com/python-injector/flask_injector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419860,"owners_count":20936012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T09:01:00.262Z","updated_at":"2025-12-12T00:40:51.631Z","avatar_url":"https://github.com/python-injector.png","language":"Python","readme":"Flask-Injector\n==============\n\n.. image:: https://codecov.io/gh/alecthomas/flask_injector/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/alecthomas/flask_injector\n\n.. image:: https://github.com/alecthomas/flask_injector/workflows/CI/badge.svg\n   :target: https://github.com/alecthomas/flask_injector?query=workflow%3ACI+branch%3Amaster\n\n\nAdds `Injector \u003chttps://github.com/alecthomas/injector\u003e`_ support to Flask,\nthis way there's no need to use global Flask objects, which makes testing simpler.\n\nInjector is a dependency-injection framework for Python, inspired by Guice. You\ncan `find Injector on PyPI \u003chttps://pypi.org/project/injector/\u003e`_ and `Injector\ndocumentation on Read the Docs \u003chttps://injector.readthedocs.io/en/latest/\u003e`_.\n\n`Flask-Injector` is compatible with CPython 3.7+.\nAs of version 0.15.0 it requires Injector version 0.20.0 or greater and Flask\n2.2.0 or greater.\n\nGitHub project page: https://github.com/alecthomas/flask_injector\n\nPyPI package page: https://pypi.org/project/Flask-Injector/\n\nChangelog: https://github.com/alecthomas/flask_injector/blob/master/CHANGELOG.rst\n\nFeatures\n--------\n\nFlask-Injector lets you inject dependencies into:\n\n* views (functions and class-based)\n* `before_request` handlers\n* `after_request` handlers\n* `teardown_request` handlers\n* template context processors\n* error handlers\n* Jinja environment globals (functions in `app.jinja_env.globals`)\n* Flask-RESTFul Resource constructors\n* Flask-RestPlus Resource constructors\n* Flask-RESTX Resource constructors\n\nFlask-Injector supports defining types using function annotations (Python 3),\nsee below.\n\nDocumentation\n-------------\n\nAs Flask-Injector uses Injector under the hood you should find the\n`Injector documentation \u003chttps://injector.readthedocs.io/en/latest/\u003e`_,\nincluding the `Injector API reference \u003chttps://injector.readthedocs.io/en/latest/api.html\u003e`_,\nhelpful. The `Injector README \u003chttps://github.com/alecthomas/injector/blob/master/README.md\u003e`_\nprovides a tutorial-level introduction to using Injector.\n\nThe Flask-Injector public API consists of the following:\n\n* `FlaskInjector` class with the constructor taking the following parameters:\n\n  * `app`, an instance of`flask.Flask` [mandatory] – the Flask application to be used\n  * `modules`, an iterable of\n    `Injector modules \u003chttps://injector.readthedocs.io/en/latest/api.html#injector.Binder.install\u003e`_ [optional]\n    – the Injector modules to be used.\n  * `injector`, an instance of\n    `injector.Injector \u003chttps://injector.readthedocs.io/en/latest/api.html#injector.Injector\u003e`_ [optional]\n    – an instance of Injector to be used if, for some reason, it's not desirable\n    for `FlaskInjector` to create a new one. You're likely to not need to use this.\n  * `request_scope_class`, an `injector.Scope \u003chttps://injector.readthedocs.io/en/latest/api.html#injector.Scope\u003e`_\n    subclass [optional] – the scope to be used instead of `RequestScope`. You're likely to need to use this\n    except for testing.\n* `RequestScope` class – an `injector.Scope \u003chttps://injector.readthedocs.io/en/latest/api.html#injector.Scope\u003e`_\n  subclass to be used for storing and reusing request-scoped dependencies\n* `request` object – to be used as a class decorator or in explicit\n  `bind() \u003chttps://injector.readthedocs.io/en/latest/api.html#injector.Binder.bind\u003e`_ calls in\n  Injector modules.\n  \nCreating an instance of `FlaskInjector` performs side-effectful configuration of the Flask\napplication passed to it. The following bindings are applied (if you want to modify them you\nneed to do it in one of the modules passed to the `FlaskInjector` constructor):\n\n* `flask.Flask` is bound to the Flask application in the (scope: singleton)\n* `flask.Config` is bound to the configuration of the Flask application\n* `flask.Request` is bound to the current Flask request object, equivalent to the thread-local\n  `flask.request` object (scope: request)\n \nExample application using Flask-Injector\n----------------------------------------\n\n.. code:: python\n\n    import sqlite3\n    from flask import Flask, Config\n    from flask.views import View\n    from flask_injector import FlaskInjector\n    from injector import inject\n\n    app = Flask(__name__)\n\n    # Configure your application by attaching views, handlers, context processors etc.:\n\n    @app.route(\"/bar\")\n    def bar():\n        return render(\"bar.html\")\n\n\n    # Route with injection\n    @app.route(\"/foo\")\n    def foo(db: sqlite3.Connection):\n        users = db.execute('SELECT * FROM users').all()\n        return render(\"foo.html\")\n\n\n    # Class-based view with injected constructor\n    class Waz(View):\n        @inject\n        def __init__(self, db: sqlite3.Connection):\n            self.db = db\n\n        def dispatch_request(self, key):\n            users = self.db.execute('SELECT * FROM users WHERE name=?', (key,)).all()\n            return 'waz'\n\n    app.add_url_rule('/waz/\u003ckey\u003e', view_func=Waz.as_view('waz'))\n\n\n    # In the Injector world, all dependency configuration and initialization is\n    # performed in modules (https://injector.readthedocs.io/en/latest/terminology.html#module).\n    # The same is true with Flask-Injector. You can see some examples of configuring\n    # Flask extensions through modules below.\n\n    # Accordingly, the next step is to create modules for any objects we want made\n    # available to the application. Note that in this example we also use the\n    # Injector to gain access to the `flask.Config`:\n\n    def configure(binder):\n        binder.bind(\n            sqlite3.Connection,\n            to=sqlite3.Connection(':memory:'),\n            scope=request,\n        )\n    \n    # Initialize Flask-Injector. This needs to be run *after* you attached all\n    # views, handlers, context processors and template globals.\n\n    FlaskInjector(app=app, modules=[configure])\n\n    # All that remains is to run the application\n\n    app.run()\n\nSee `example.py` for a more complete example, including `Flask-SQLAlchemy` and\n`Flask-Cache` integration.\n\nSupporting Flask Extensions\n---------------------------\n\nTypically, Flask extensions are initialized at the global scope using a\npattern similar to the following.\n\n.. code:: python\n\n    app = Flask(__name__)\n    ext = ExtClass(app)\n\n    @app.route(...)\n    def view():\n        # Use ext object here...\n\nAs we don't have these globals with Flask-Injector we have to configure the\nextension the Injector way - through modules. Modules can either be subclasses\nof `injector.Module` or a callable taking an `injector.Binder` instance.\n\n.. code:: python\n\n    from injector import Module\n\n    class MyModule(Module):\n        @provider\n        @singleton\n        def provide_ext(self, app: Flask) -\u003e ExtClass:\n            return ExtClass(app)\n\n    def main():\n        app = Flask(__name__)\n        app.config.update(\n            EXT_CONFIG_VAR='some_value',\n        )\n\n        # attach your views etc. here\n\n        FlaskInjector(app=app, modules=[MyModule])\n\n        app.run()\n\n*Make sure to bind extension objects as singletons.*\n","funding_links":[],"categories":["Software","Third-Party Extensions","Flask Utilities","Python"],"sub_categories":["Integration with web frameworks","Utils"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-injector%2Fflask_injector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpython-injector%2Fflask_injector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-injector%2Fflask_injector/lists"}