From 092be74145ecfaf1ed06cf17f31e65e3f6e688c3 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Sun, 15 Nov 2020 18:07:57 -0500 Subject: [PATCH] Add docs page on flask blueprints example --- docs/examples/flask-blueprints.rst | 89 ++++++++++++++++++++++++++++++ docs/examples/index.rst | 1 + docs/introduction/di_in_python.rst | 1 + docs/wiring.rst | 1 + 4 files changed, 92 insertions(+) create mode 100644 docs/examples/flask-blueprints.rst diff --git a/docs/examples/flask-blueprints.rst b/docs/examples/flask-blueprints.rst new file mode 100644 index 00000000..c3a8f14f --- /dev/null +++ b/docs/examples/flask-blueprints.rst @@ -0,0 +1,89 @@ +.. _flask-blueprints-example: + +Flask blueprints example +======================== + +.. meta:: + :keywords: Python,Dependency Injection,Flask,Blueprints,Example + :description: This example demonstrates a usage of the Flask Blueprints and Dependency Injector. + + +This example shows how to use ``Dependency Injector`` with `Flask `_ +blueprints. + +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 `_. + +Application structure +--------------------- + +Application has next structure: + +.. code-block:: bash + + ./ + ├── githubnavigator/ + │ ├── blueprints + │ │ ├── __init__.py + │ │ └── example.py + │ ├── templates + │ │ ├── base.html + │ │ └── index.py + │ ├── __init__.py + │ ├── application.py + │ ├── containers.py + │ ├── services.py + │ └── tests.py + ├── config.yml + └── requirements.txt + +Container +--------- + +Declarative container is defined in ``githubnavigator/containers.py``: + +.. literalinclude:: ../../examples/miniapps/flask-blueprints/githubnavigator/containers.py + :language: python + +Blueprints +---------- + +Blueprint's view has dependencies on search service and some config options. The dependencies are injected +using :ref:`wiring` feature. + +Listing of ``githubnavigator/blueprints/example.py``: + +.. literalinclude:: ../../examples/miniapps/flask-blueprints/githubnavigator/blueprints/example.py + :language: python + +Application factory +------------------- + +Application factory creates container, wires it with the blueprints, creates +``Flask`` app, and setup routes. + +Listing of ``githubnavigator/application.py``: + +.. literalinclude:: ../../examples/miniapps/flask-blueprints/githubnavigator/application.py + :language: python + +Tests +----- + +Tests use :ref:`provider-overriding` feature to replace github client with a mock ``githubnavigator/tests.py``: + +.. literalinclude:: ../../examples/miniapps/flask-blueprints/githubnavigator/tests.py + :language: python + :emphasize-lines: 44,67 + +Sources +------- + +Explore the sources on the `Github `_. + +.. disqus:: diff --git a/docs/examples/index.rst b/docs/examples/index.rst index 9b7a1134..bbb2feb3 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -15,6 +15,7 @@ Explore the examples to see the ``Dependency Injector`` in action. decoupled-packages django flask + flask-blueprints aiohttp sanic fastapi diff --git a/docs/introduction/di_in_python.rst b/docs/introduction/di_in_python.rst index e7a5f81e..1a1daf07 100644 --- a/docs/introduction/di_in_python.rst +++ b/docs/introduction/di_in_python.rst @@ -283,6 +283,7 @@ Choose one of the following as a next step: - :ref:`decoupled-packages` - :ref:`django-example` - :ref:`flask-example` + - :ref:`flask-blueprints-example` - :ref:`aiohttp-example` - :ref:`sanic-example` - :ref:`fastapi-example` diff --git a/docs/wiring.rst b/docs/wiring.rst index 0db27a5d..b5ce31d8 100644 --- a/docs/wiring.rst +++ b/docs/wiring.rst @@ -207,6 +207,7 @@ Take a look at other application examples: - :ref:`decoupled-packages` - :ref:`django-example` - :ref:`flask-example` +- :ref:`flask-blueprints-example` - :ref:`aiohttp-example` - :ref:`sanic-example` - :ref:`fastapi-example`