diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 4973e63e..3ca93d4c 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -11,6 +11,7 @@ follows `Semantic versioning`_ ----- - Add ``wiring`` feature. - Update ``aiohttp`` example. +- Update ``flask`` example. 3.44.0 ------ diff --git a/examples/miniapps/ghnav-flask/README.rst b/examples/miniapps/flask/README.rst similarity index 86% rename from examples/miniapps/ghnav-flask/README.rst rename to examples/miniapps/flask/README.rst index 70d75185..12bd3dcc 100644 --- a/examples/miniapps/ghnav-flask/README.rst +++ b/examples/miniapps/flask/README.rst @@ -1,8 +1,8 @@ -Flask Dependency Injection Example -================================== +Dependency Injector + Flask Example +=================================== Application ``githubnavigator`` is a `Flask `_ + -`Dependency Injector `_ application. +`Dependency Injector `_ example application. .. image:: screenshot.png @@ -90,10 +90,10 @@ The output should be something like: Name Stmts Miss Cover ---------------------------------------------------- githubnavigator/__init__.py 0 0 100% - githubnavigator/application.py 11 0 100% - githubnavigator/containers.py 13 0 100% + githubnavigator/application.py 15 0 100% + githubnavigator/containers.py 7 0 100% githubnavigator/services.py 14 0 100% - githubnavigator/tests.py 32 0 100% - githubnavigator/views.py 7 0 100% + githubnavigator/tests.py 34 0 100% + githubnavigator/views.py 9 0 100% ---------------------------------------------------- - TOTAL 77 0 100% + TOTAL 79 0 100% diff --git a/examples/miniapps/flask/config.yml b/examples/miniapps/flask/config.yml new file mode 100644 index 00000000..938c609a --- /dev/null +++ b/examples/miniapps/flask/config.yml @@ -0,0 +1,5 @@ +github: + request_timeout: 10 +default: + query: "Dependency Injector" + limit: 10 diff --git a/examples/miniapps/ghnav-flask/githubnavigator/__init__.py b/examples/miniapps/flask/githubnavigator/__init__.py similarity index 100% rename from examples/miniapps/ghnav-flask/githubnavigator/__init__.py rename to examples/miniapps/flask/githubnavigator/__init__.py diff --git a/examples/miniapps/flask/githubnavigator/application.py b/examples/miniapps/flask/githubnavigator/application.py new file mode 100644 index 00000000..28f1da36 --- /dev/null +++ b/examples/miniapps/flask/githubnavigator/application.py @@ -0,0 +1,25 @@ +"""Application module.""" + +from flask import Flask +from flask_bootstrap import Bootstrap + +from .containers import Container +from . import views + + +def create_app(): + container = Container() + container.config.from_yaml('config.yml') + container.config.github.auth_token.from_env('GITHUB_TOKEN') + + container.wire(modules=[views]) + + app = Flask(__name__) + app.container = container + + bootstrap = Bootstrap() + bootstrap.init_app(app) + + app.add_url_rule('/', 'index', views.index) + + return app diff --git a/examples/miniapps/flask/githubnavigator/containers.py b/examples/miniapps/flask/githubnavigator/containers.py new file mode 100644 index 00000000..f88c7086 --- /dev/null +++ b/examples/miniapps/flask/githubnavigator/containers.py @@ -0,0 +1,22 @@ +"""Application containers module.""" + +from dependency_injector import containers, providers +from github import Github + +from . import services + + +class Container(containers.DeclarativeContainer): + + config = providers.Configuration() + + github_client = providers.Factory( + Github, + login_or_token=config.github.auth_token, + timeout=config.github.request_timeout, + ) + + search_service = providers.Factory( + services.SearchService, + github_client=github_client, + ) diff --git a/examples/miniapps/ghnav-flask/githubnavigator/services.py b/examples/miniapps/flask/githubnavigator/services.py similarity index 100% rename from examples/miniapps/ghnav-flask/githubnavigator/services.py rename to examples/miniapps/flask/githubnavigator/services.py diff --git a/examples/miniapps/ghnav-flask/githubnavigator/templates/base.html b/examples/miniapps/flask/githubnavigator/templates/base.html similarity index 100% rename from examples/miniapps/ghnav-flask/githubnavigator/templates/base.html rename to examples/miniapps/flask/githubnavigator/templates/base.html diff --git a/examples/miniapps/ghnav-flask/githubnavigator/templates/index.html b/examples/miniapps/flask/githubnavigator/templates/index.html similarity index 100% rename from examples/miniapps/ghnav-flask/githubnavigator/templates/index.html rename to examples/miniapps/flask/githubnavigator/templates/index.html diff --git a/examples/miniapps/ghnav-flask/githubnavigator/tests.py b/examples/miniapps/flask/githubnavigator/tests.py similarity index 96% rename from examples/miniapps/ghnav-flask/githubnavigator/tests.py rename to examples/miniapps/flask/githubnavigator/tests.py index f8ea3d87..a8618467 100644 --- a/examples/miniapps/ghnav-flask/githubnavigator/tests.py +++ b/examples/miniapps/flask/githubnavigator/tests.py @@ -11,7 +11,9 @@ from .application import create_app @pytest.fixture def app(): - return create_app() + app = create_app() + yield app + app.container.unwire() def test_index(client, app): diff --git a/examples/miniapps/ghnav-flask/githubnavigator/views.py b/examples/miniapps/flask/githubnavigator/views.py similarity index 58% rename from examples/miniapps/ghnav-flask/githubnavigator/views.py rename to examples/miniapps/flask/githubnavigator/views.py index 9d97001c..22a3d35d 100644 --- a/examples/miniapps/ghnav-flask/githubnavigator/views.py +++ b/examples/miniapps/flask/githubnavigator/views.py @@ -1,14 +1,16 @@ """Views module.""" from flask import request, render_template +from dependency_injector.wiring import Provide from .services import SearchService +from .containers import Container def index( - search_service: SearchService, - default_query: str, - default_limit: int, + search_service: SearchService = Provide[Container.search_service], + default_query: str = Provide[Container.config.default.query], + default_limit: int = Provide[Container.config.default.limit.as_int()], ): query = request.args.get('query', default_query) limit = request.args.get('limit', default_limit, int) diff --git a/examples/miniapps/ghnav-flask/requirements.txt b/examples/miniapps/flask/requirements.txt similarity index 100% rename from examples/miniapps/ghnav-flask/requirements.txt rename to examples/miniapps/flask/requirements.txt diff --git a/examples/miniapps/ghnav-flask/screenshot.png b/examples/miniapps/flask/screenshot.png similarity index 100% rename from examples/miniapps/ghnav-flask/screenshot.png rename to examples/miniapps/flask/screenshot.png diff --git a/examples/miniapps/ghnav-flask/config.yml b/examples/miniapps/ghnav-flask/config.yml deleted file mode 100644 index 193fc06e..00000000 --- a/examples/miniapps/ghnav-flask/config.yml +++ /dev/null @@ -1,5 +0,0 @@ -github: - request_timeout: 10 -search: - default_query: "Dependency Injector" - default_limit: 10 diff --git a/examples/miniapps/ghnav-flask/githubnavigator/application.py b/examples/miniapps/ghnav-flask/githubnavigator/application.py deleted file mode 100644 index 297c5812..00000000 --- a/examples/miniapps/ghnav-flask/githubnavigator/application.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Application module.""" - -from .containers import ApplicationContainer - - -def create_app(): - """Create and return Flask application.""" - container = ApplicationContainer() - container.config.from_yaml('config.yml') - container.config.github.auth_token.from_env('GITHUB_TOKEN') - - app = container.app() - app.container = container - - bootstrap = container.bootstrap() - bootstrap.init_app(app) - - app.add_url_rule('/', view_func=container.index_view.as_view()) - - return app diff --git a/examples/miniapps/ghnav-flask/githubnavigator/containers.py b/examples/miniapps/ghnav-flask/githubnavigator/containers.py deleted file mode 100644 index 1bd47748..00000000 --- a/examples/miniapps/ghnav-flask/githubnavigator/containers.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Application containers module.""" - -from dependency_injector import containers, providers -from dependency_injector.ext import flask -from flask import Flask -from flask_bootstrap import Bootstrap -from github import Github - -from . import views, services - - -class ApplicationContainer(containers.DeclarativeContainer): - """Application container.""" - - app = flask.Application(Flask, __name__) - - bootstrap = flask.Extension(Bootstrap) - - config = providers.Configuration() - - github_client = providers.Factory( - Github, - login_or_token=config.github.auth_token, - timeout=config.github.request_timeout, - ) - - search_service = providers.Factory( - services.SearchService, - github_client=github_client, - ) - - index_view = flask.View( - views.index, - search_service=search_service, - default_query=config.search.default_query, - default_limit=config.search.default_limit, - )