diff --git a/docs/examples/flask.rst b/docs/examples/flask.rst new file mode 100644 index 00000000..f4750ced --- /dev/null +++ b/docs/examples/flask.rst @@ -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 `_. + +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 `_. + +: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 `_. + +.. disqus:: diff --git a/docs/examples/images/flask.png b/docs/examples/images/flask.png new file mode 100644 index 00000000..350aaa67 Binary files /dev/null and b/docs/examples/images/flask.png differ diff --git a/docs/examples/index.rst b/docs/examples/index.rst index 144afecc..452ff8f5 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -14,6 +14,7 @@ Explore the examples to see the ``Dependency Injector`` in action. application-multiple-containers decoupled-packages django + flask sanic .. disqus:: diff --git a/docs/examples/sanic.rst b/docs/examples/sanic.rst index af0e20a9..4a15f957 100644 --- a/docs/examples/sanic.rst +++ b/docs/examples/sanic.rst @@ -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