mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-22 21:46:17 +03:00
Add flask example to the docs
This commit is contained in:
parent
68334008e5
commit
5f66701241
85
docs/examples/flask.rst
Normal file
85
docs/examples/flask.rst
Normal file
|
@ -0,0 +1,85 @@
|
|||
Flask example
|
||||
=============
|
||||
|
||||
.. meta::
|
||||
:keywords: Python,Dependency Injection,Flask,Example
|
||||
:description: This example demonstrates a usage of the Flask and Dependency Injector.
|
||||
|
||||
|
||||
This example shows how to use ``Dependency Injector`` with `Flask <https://flask.palletsprojects.com/en/1.1.x/>`_.
|
||||
|
||||
The example application helps to search for repositories on the Github.
|
||||
|
||||
.. image:: images/flask.png
|
||||
:width: 100%
|
||||
:align: center
|
||||
|
||||
The source code is available on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/flask>`_.
|
||||
|
||||
:ref:`flask-tutorial` demonstrates how to build this application step-by-step.
|
||||
|
||||
Application structure
|
||||
---------------------
|
||||
|
||||
Application has next structure:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
./
|
||||
├── githubnavigator/
|
||||
│ ├── templates
|
||||
│ │ ├── base.html
|
||||
│ │ └── index.py
|
||||
│ ├── __init__.py
|
||||
│ ├── application.py
|
||||
│ ├── containers.py
|
||||
│ ├── services.py
|
||||
│ ├── tests.py
|
||||
│ └── views.py
|
||||
├── config.yml
|
||||
└── requirements.txt
|
||||
|
||||
Container
|
||||
---------
|
||||
|
||||
Declarative container is defined in ``githubnavigator/containers.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/miniapps/flask/githubnavigator/containers.py
|
||||
:language: python
|
||||
|
||||
Views
|
||||
-----
|
||||
|
||||
View has dependencies on search service and some config options. The dependencies are injected
|
||||
using :ref:`wiring` feature.
|
||||
|
||||
Listing of ``githubnavigator/views.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/miniapps/flask/githubnavigator/views.py
|
||||
:language: python
|
||||
|
||||
Application factory
|
||||
-------------------
|
||||
Application factory creates container, wires it with the ``views`` module, creates
|
||||
``Flask`` app and setup routes.
|
||||
|
||||
Listing of ``githubnavigator/application.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/miniapps/flask/githubnavigator/application.py
|
||||
:language: python
|
||||
|
||||
Tests
|
||||
-----
|
||||
|
||||
Tests use :ref:`provider-overriding` feature to replace search service with a mock ``githubnavigator/tests.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/miniapps/flask/githubnavigator/tests.py
|
||||
:language: python
|
||||
:emphasize-lines: 44,67
|
||||
|
||||
Sources
|
||||
-------
|
||||
|
||||
Explore the sources on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/flask>`_.
|
||||
|
||||
.. disqus::
|
BIN
docs/examples/images/flask.png
Normal file
BIN
docs/examples/images/flask.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 647 KiB |
|
@ -14,6 +14,7 @@ Explore the examples to see the ``Dependency Injector`` in action.
|
|||
application-multiple-containers
|
||||
decoupled-packages
|
||||
django
|
||||
flask
|
||||
sanic
|
||||
|
||||
.. disqus::
|
||||
|
|
|
@ -54,7 +54,7 @@ Listing of ``giphynavigator/handlers.py``:
|
|||
Application factory
|
||||
-------------------
|
||||
Application factory creates container, wires it with the ``handlers`` module, creates
|
||||
``Sanic`` app instance and setup routes.
|
||||
``Sanic`` app and setup routes.
|
||||
|
||||
Listing of ``giphynavigator/application.py``:
|
||||
|
||||
|
@ -64,7 +64,7 @@ Listing of ``giphynavigator/application.py``:
|
|||
Tests
|
||||
-----
|
||||
|
||||
Tests use :ref:`provider-overriding` feature to replace search service with a mock ``web/giphynavigator.py``:
|
||||
Tests use :ref:`provider-overriding` feature to replace search service with a mock ``giphynavigator/tests.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/miniapps/sanic/giphynavigator/tests.py
|
||||
:language: python
|
||||
|
|
Loading…
Reference in New Issue
Block a user