mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-22 21:46:17 +03:00
Update flask example and tutorial
This commit is contained in:
parent
15b32f9c01
commit
7a6ff43b2c
|
@ -707,17 +707,19 @@ Let's inject ``SearchService`` into the ``index`` view. We will use :ref:`Wiring
|
||||||
Edit ``views.py``:
|
Edit ``views.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 4,6-7,10,14
|
:emphasize-lines: 4,6-7,10-11,15
|
||||||
|
:emphasize-lines: 4,6-7,10-11,15
|
||||||
|
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
from flask import request, render_template
|
from flask import request, render_template
|
||||||
from dependency_injector.wiring import Provide
|
from dependency_injector.wiring import inject, Provide
|
||||||
|
|
||||||
from .services import SearchService
|
from .services import SearchService
|
||||||
from .containers import Container
|
from .containers import Container
|
||||||
|
|
||||||
|
|
||||||
|
@inject
|
||||||
def index(search_service: SearchService = Provide[Container.search_service]):
|
def index(search_service: SearchService = Provide[Container.search_service]):
|
||||||
query = request.args.get('query', 'Dependency Injector')
|
query = request.args.get('query', 'Dependency Injector')
|
||||||
limit = request.args.get('limit', 10, int)
|
limit = request.args.get('limit', 10, int)
|
||||||
|
@ -783,17 +785,18 @@ Let's make some refactoring. We will move these values to the config.
|
||||||
Edit ``views.py``:
|
Edit ``views.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 10-16
|
:emphasize-lines: 11-17
|
||||||
|
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
from flask import request, render_template
|
from flask import request, render_template
|
||||||
from dependency_injector.wiring import Provide
|
from dependency_injector.wiring import inject, Provide
|
||||||
|
|
||||||
from .services import SearchService
|
from .services import SearchService
|
||||||
from .containers import Container
|
from .containers import Container
|
||||||
|
|
||||||
|
|
||||||
|
@inject
|
||||||
def index(
|
def index(
|
||||||
search_service: SearchService = Provide[Container.search_service],
|
search_service: SearchService = Provide[Container.search_service],
|
||||||
default_query: str = Provide[Container.config.default.query],
|
default_query: str = Provide[Container.config.default.query],
|
||||||
|
@ -972,9 +975,9 @@ You should see:
|
||||||
githubnavigator/containers.py 7 0 100%
|
githubnavigator/containers.py 7 0 100%
|
||||||
githubnavigator/services.py 14 0 100%
|
githubnavigator/services.py 14 0 100%
|
||||||
githubnavigator/tests.py 34 0 100%
|
githubnavigator/tests.py 34 0 100%
|
||||||
githubnavigator/views.py 9 0 100%
|
githubnavigator/views.py 10 0 100%
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
TOTAL 79 0 100%
|
TOTAL 80 0 100%
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
|
|
|
@ -95,6 +95,6 @@ The output should be something like:
|
||||||
githubnavigator/containers.py 7 0 100%
|
githubnavigator/containers.py 7 0 100%
|
||||||
githubnavigator/services.py 14 0 100%
|
githubnavigator/services.py 14 0 100%
|
||||||
githubnavigator/tests.py 34 0 100%
|
githubnavigator/tests.py 34 0 100%
|
||||||
githubnavigator/views.py 9 0 100%
|
githubnavigator/views.py 10 0 100%
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
TOTAL 79 0 100%
|
TOTAL 80 0 100%
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
from flask import request, render_template
|
from flask import request, render_template
|
||||||
from dependency_injector.wiring import Provide
|
from dependency_injector.wiring import inject, Provide
|
||||||
|
|
||||||
from .services import SearchService
|
from .services import SearchService
|
||||||
from .containers import Container
|
from .containers import Container
|
||||||
|
|
||||||
|
|
||||||
|
@inject
|
||||||
def index(
|
def index(
|
||||||
search_service: SearchService = Provide[Container.search_service],
|
search_service: SearchService = Provide[Container.search_service],
|
||||||
default_query: str = Provide[Container.config.default.query],
|
default_query: str = Provide[Container.config.default.query],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user