Skip to content

Commit 170819c

Browse files
committed
Update flask example
1 parent 4fab71c commit 170819c

File tree

16 files changed

+69
-74
lines changed

16 files changed

+69
-74
lines changed

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ follows `Semantic versioning`_
1111
-----
1212
- Add ``wiring`` feature.
1313
- Update ``aiohttp`` example.
14+
- Update ``flask`` example.
1415

1516
3.44.0
1617
------

examples/miniapps/ghnav-flask/README.rst renamed to examples/miniapps/flask/README.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Flask Dependency Injection Example
2-
==================================
1+
Dependency Injector + Flask Example
2+
===================================
33

44
Application ``githubnavigator`` is a `Flask <https://flask.palletsprojects.com/>`_ +
5-
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ application.
5+
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ example application.
66

77
.. image:: screenshot.png
88

@@ -90,10 +90,10 @@ The output should be something like:
9090
Name Stmts Miss Cover
9191
----------------------------------------------------
9292
githubnavigator/__init__.py 0 0 100%
93-
githubnavigator/application.py 11 0 100%
94-
githubnavigator/containers.py 13 0 100%
93+
githubnavigator/application.py 15 0 100%
94+
githubnavigator/containers.py 7 0 100%
9595
githubnavigator/services.py 14 0 100%
96-
githubnavigator/tests.py 32 0 100%
97-
githubnavigator/views.py 7 0 100%
96+
githubnavigator/tests.py 34 0 100%
97+
githubnavigator/views.py 9 0 100%
9898
----------------------------------------------------
99-
TOTAL 77 0 100%
99+
TOTAL 79 0 100%

examples/miniapps/flask/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
github:
2+
request_timeout: 10
3+
default:
4+
query: "Dependency Injector"
5+
limit: 10
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Application module."""
2+
3+
from flask import Flask
4+
from flask_bootstrap import Bootstrap
5+
6+
from .containers import Container
7+
from . import views
8+
9+
10+
def create_app():
11+
container = Container()
12+
container.config.from_yaml('config.yml')
13+
container.config.github.auth_token.from_env('GITHUB_TOKEN')
14+
15+
container.wire(modules=[views])
16+
17+
app = Flask(__name__)
18+
app.container = container
19+
20+
bootstrap = Bootstrap()
21+
bootstrap.init_app(app)
22+
23+
app.add_url_rule('/', 'index', views.index)
24+
25+
return app
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Application containers module."""
2+
3+
from dependency_injector import containers, providers
4+
from github import Github
5+
6+
from . import services
7+
8+
9+
class Container(containers.DeclarativeContainer):
10+
11+
config = providers.Configuration()
12+
13+
github_client = providers.Factory(
14+
Github,
15+
login_or_token=config.github.auth_token,
16+
timeout=config.github.request_timeout,
17+
)
18+
19+
search_service = providers.Factory(
20+
services.SearchService,
21+
github_client=github_client,
22+
)

examples/miniapps/ghnav-flask/githubnavigator/tests.py renamed to examples/miniapps/flask/githubnavigator/tests.py

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

1212
@pytest.fixture
1313
def app():
14-
return create_app()
14+
app = create_app()
15+
yield app
16+
app.container.unwire()
1517

1618

1719
def test_index(client, app):

0 commit comments

Comments
 (0)