mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-31 11:51:39 +03:00
Update flask example
This commit is contained in:
parent
4fab71c35b
commit
170819c6ed
|
@ -11,6 +11,7 @@ follows `Semantic versioning`_
|
||||||
-----
|
-----
|
||||||
- Add ``wiring`` feature.
|
- Add ``wiring`` feature.
|
||||||
- Update ``aiohttp`` example.
|
- Update ``aiohttp`` example.
|
||||||
|
- Update ``flask`` example.
|
||||||
|
|
||||||
3.44.0
|
3.44.0
|
||||||
------
|
------
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
Flask Dependency Injection Example
|
Dependency Injector + Flask Example
|
||||||
==================================
|
===================================
|
||||||
|
|
||||||
Application ``githubnavigator`` is a `Flask <https://flask.palletsprojects.com/>`_ +
|
Application ``githubnavigator`` is a `Flask <https://flask.palletsprojects.com/>`_ +
|
||||||
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ application.
|
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ example application.
|
||||||
|
|
||||||
.. image:: screenshot.png
|
.. image:: screenshot.png
|
||||||
|
|
||||||
|
@ -90,10 +90,10 @@ The output should be something like:
|
||||||
Name Stmts Miss Cover
|
Name Stmts Miss Cover
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
githubnavigator/__init__.py 0 0 100%
|
githubnavigator/__init__.py 0 0 100%
|
||||||
githubnavigator/application.py 11 0 100%
|
githubnavigator/application.py 15 0 100%
|
||||||
githubnavigator/containers.py 13 0 100%
|
githubnavigator/containers.py 7 0 100%
|
||||||
githubnavigator/services.py 14 0 100%
|
githubnavigator/services.py 14 0 100%
|
||||||
githubnavigator/tests.py 32 0 100%
|
githubnavigator/tests.py 34 0 100%
|
||||||
githubnavigator/views.py 7 0 100%
|
githubnavigator/views.py 9 0 100%
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
TOTAL 77 0 100%
|
TOTAL 79 0 100%
|
5
examples/miniapps/flask/config.yml
Normal file
5
examples/miniapps/flask/config.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
github:
|
||||||
|
request_timeout: 10
|
||||||
|
default:
|
||||||
|
query: "Dependency Injector"
|
||||||
|
limit: 10
|
25
examples/miniapps/flask/githubnavigator/application.py
Normal file
25
examples/miniapps/flask/githubnavigator/application.py
Normal file
|
@ -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
|
22
examples/miniapps/flask/githubnavigator/containers.py
Normal file
22
examples/miniapps/flask/githubnavigator/containers.py
Normal file
|
@ -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,
|
||||||
|
)
|
|
@ -11,7 +11,9 @@ from .application import create_app
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def app():
|
def app():
|
||||||
return create_app()
|
app = create_app()
|
||||||
|
yield app
|
||||||
|
app.container.unwire()
|
||||||
|
|
||||||
|
|
||||||
def test_index(client, app):
|
def test_index(client, app):
|
|
@ -1,14 +1,16 @@
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
from flask import request, render_template
|
from flask import request, render_template
|
||||||
|
from dependency_injector.wiring import Provide
|
||||||
|
|
||||||
from .services import SearchService
|
from .services import SearchService
|
||||||
|
from .containers import Container
|
||||||
|
|
||||||
|
|
||||||
def index(
|
def index(
|
||||||
search_service: SearchService,
|
search_service: SearchService = Provide[Container.search_service],
|
||||||
default_query: str,
|
default_query: str = Provide[Container.config.default.query],
|
||||||
default_limit: int,
|
default_limit: int = Provide[Container.config.default.limit.as_int()],
|
||||||
):
|
):
|
||||||
query = request.args.get('query', default_query)
|
query = request.args.get('query', default_query)
|
||||||
limit = request.args.get('limit', default_limit, int)
|
limit = request.args.get('limit', default_limit, int)
|
Before Width: | Height: | Size: 647 KiB After Width: | Height: | Size: 647 KiB |
|
@ -1,5 +0,0 @@
|
||||||
github:
|
|
||||||
request_timeout: 10
|
|
||||||
search:
|
|
||||||
default_query: "Dependency Injector"
|
|
||||||
default_limit: 10
|
|
|
@ -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
|
|
|
@ -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,
|
|
||||||
)
|
|
Loading…
Reference in New Issue
Block a user