Skip to content

Commit f531de3

Browse files
committed
Update flask example and tutorial
1 parent 2ce4a1c commit f531de3

File tree

4 files changed

+49
-61
lines changed

4 files changed

+49
-61
lines changed

docs/tutorials/flask.rst

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ You should see something like:
112112
(venv) $ python -c "import dependency_injector; print(dependency_injector.__version__)"
113113
4.37.0
114114
(venv) $ python -c "import flask; print(flask.__version__)"
115-
1.1.2
115+
2.0.2
116116
117117
*Versions can be different. That's fine.*
118118

@@ -444,9 +444,10 @@ and run in the terminal:
444444
Now we need to add Github API client the container. We will need to add two more providers from
445445
the ``dependency_injector.providers`` module:
446446

447-
- ``Factory`` provider that will create ``Github`` client.
448-
- ``Configuration`` provider that will be used for providing the API token and the request timeout
449-
for the ``Github`` client.
447+
- ``Factory`` provider. It will create a ``Github`` client.
448+
- ``Configuration`` provider. It will provide an API token and a request timeout for the ``Github`` client.
449+
We will specify the location of the configuration file. The configuration provider will parse
450+
the configuration file when we create a container instance.
450451

451452
Edit ``containers.py``:
452453

@@ -461,31 +462,22 @@ Edit ``containers.py``:
461462
462463
class Container(containers.DeclarativeContainer):
463464
464-
config = providers.Configuration()
465+
config = providers.Configuration(yaml_files=["config.yml"])
465466
466467
github_client = providers.Factory(
467468
Github,
468469
login_or_token=config.github.auth_token,
469470
timeout=config.github.request_timeout,
470471
)
471472
472-
.. note::
473-
474-
We have used the configuration value before it was defined. That's the principle how
475-
``Configuration`` provider works.
476-
477-
Use first, define later.
478473
479474
.. note::
480475

481476
Don't forget to remove the Ellipsis ``...`` from the container. We don't need it anymore
482477
since we container is not empty.
483478

484-
Now let's add the configuration file.
485-
486-
We will use YAML.
487-
488-
Create an empty file ``config.yml`` in the root of the project:
479+
Now let's add the configuration file. We will use YAML. Create an empty file ``config.yml``
480+
in the root of the project:
489481

490482
.. code-block:: bash
491483
:emphasize-lines: 11
@@ -530,17 +522,13 @@ and install it:
530522
531523
pip install -r requirements.txt
532524
533-
We will use environment variable ``GITHUB_TOKEN`` to provide the API token.
534-
535-
Now we need to edit ``create_app()`` to make two things when application starts:
536-
537-
- Load the configuration file the ``config.yml``.
538-
- Load the API token from the ``GITHUB_TOKEN`` environment variable.
525+
We will use the ``GITHUB_TOKEN`` environment variable to provide the API token. Let's edit
526+
``create_app()`` to fetch the token value from it.
539527

540528
Edit ``application.py``:
541529

542530
.. code-block:: python
543-
:emphasize-lines: 12-13
531+
:emphasize-lines: 12
544532
545533
"""Application module."""
546534
@@ -553,7 +541,6 @@ Edit ``application.py``:
553541
554542
def create_app() -> Flask:
555543
container = Container()
556-
container.config.from_yaml("config.yml")
557544
container.config.github.auth_token.from_env("GITHUB_TOKEN")
558545
559546
app = Flask(__name__)
@@ -684,7 +671,7 @@ Edit ``containers.py``:
684671
685672
class Container(containers.DeclarativeContainer):
686673
687-
config = providers.Configuration()
674+
config = providers.Configuration(yaml_files=["config.yml"])
688675
689676
github_client = providers.Factory(
690677
Github,
@@ -732,38 +719,39 @@ Edit ``views.py``:
732719
repositories=repositories,
733720
)
734721
735-
To make the injection work we need to wire the container instance with the ``views`` module.
736-
This needs to be done once. After it's done we can use ``Provide`` markers to specify as many
737-
injections as needed for any view.
722+
To make the injection work we need to wire the container with the ``views`` module.
723+
Let's configure the container to automatically make wiring with the ``views`` module when we
724+
create a container instance.
738725

739-
Edit ``application.py``:
726+
Edit ``containers.py``:
740727

741728
.. code-block:: python
742-
:emphasize-lines: 14
729+
:emphasize-lines: 11
743730
744-
"""Application module."""
731+
"""Containers module."""
745732
746-
from flask import Flask
747-
from flask_bootstrap import Bootstrap
733+
from dependency_injector import containers, providers
734+
from github import Github
748735
749-
from .containers import Container
750-
from . import views
736+
from . import services
751737
752738
753-
def create_app() -> Flask:
754-
container = Container()
755-
container.config.from_yaml("config.yml")
756-
container.config.github.auth_token.from_env("GITHUB_TOKEN")
757-
container.wire(modules=[views])
739+
class Container(containers.DeclarativeContainer):
758740
759-
app = Flask(__name__)
760-
app.container = container
761-
app.add_url_rule("/", "index", views.index)
741+
wiring_config = containers.WiringConfiguration(modules=[".views"])
762742
763-
bootstrap = Bootstrap()
764-
bootstrap.init_app(app)
743+
config = providers.Configuration(yaml_files=["config.yml"])
765744
766-
return app
745+
github_client = providers.Factory(
746+
Github,
747+
login_or_token=config.github.auth_token,
748+
timeout=config.github.request_timeout,
749+
)
750+
751+
search_service = providers.Factory(
752+
services.SearchService,
753+
github_client=github_client,
754+
)
767755
768756
Make sure the app is running or use ``flask run`` and open ``http://127.0.0.1:5000/``.
769757

@@ -960,23 +948,23 @@ You should see:
960948

961949
.. code-block:: bash
962950
963-
platform darwin -- Python 3.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
964-
plugins: flask-1.0.0, cov-2.10.0
951+
platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
952+
plugins: cov-3.0.0, flask-1.2.0
965953
collected 2 items
966954
967955
githubnavigator/tests.py .. [100%]
968956
969-
---------- coverage: platform darwin, python 3.9 -----------
957+
---------- coverage: platform darwin, python 3.10.0-final-0 ----------
970958
Name Stmts Miss Cover
971959
----------------------------------------------------
972960
githubnavigator/__init__.py 0 0 100%
973-
githubnavigator/application.py 15 0 100%
974-
githubnavigator/containers.py 7 0 100%
961+
githubnavigator/application.py 13 0 100%
962+
githubnavigator/containers.py 8 0 100%
975963
githubnavigator/services.py 14 0 100%
976964
githubnavigator/tests.py 34 0 100%
977965
githubnavigator/views.py 10 0 100%
978966
----------------------------------------------------
979-
TOTAL 80 0 100%
967+
TOTAL 79 0 100%
980968
981969
.. note::
982970

examples/miniapps/flask/README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ The output should be something like:
8181

8282
.. code-block::
8383
84-
platform darwin -- Python 3.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
85-
plugins: flask-1.0.0, cov-2.10.0
84+
platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
85+
plugins: cov-3.0.0, flask-1.2.0
8686
collected 2 items
8787
8888
githubnavigator/tests.py .. [100%]
8989
90-
---------- coverage: platform darwin, python 3.9 -----------
90+
---------- coverage: platform darwin, python 3.10.0-final-0 ----------
9191
Name Stmts Miss Cover
9292
----------------------------------------------------
9393
githubnavigator/__init__.py 0 0 100%
94-
githubnavigator/application.py 15 0 100%
95-
githubnavigator/containers.py 7 0 100%
94+
githubnavigator/application.py 13 0 100%
95+
githubnavigator/containers.py 8 0 100%
9696
githubnavigator/services.py 14 0 100%
9797
githubnavigator/tests.py 34 0 100%
9898
githubnavigator/views.py 10 0 100%
9999
----------------------------------------------------
100-
TOTAL 80 0 100%
100+
TOTAL 79 0 100%

examples/miniapps/flask/githubnavigator/application.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
def create_app() -> Flask:
1111
container = Container()
12-
container.config.from_yaml("config.yml")
1312
container.config.github.auth_token.from_env("GITHUB_TOKEN")
14-
container.wire(modules=[views])
1513

1614
app = Flask(__name__)
1715
app.container = container

examples/miniapps/flask/githubnavigator/containers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
class Container(containers.DeclarativeContainer):
1010

11-
config = providers.Configuration()
11+
wiring_config = containers.WiringConfiguration(modules=[".views"])
12+
13+
config = providers.Configuration(yaml_files=["config.yml"])
1214

1315
github_client = providers.Factory(
1416
Github,

0 commit comments

Comments
 (0)