Add docs page on flask blueprints example

This commit is contained in:
Roman Mogylatov 2020-11-15 18:07:57 -05:00
parent ca671abea6
commit 092be74145
4 changed files with 92 additions and 0 deletions

View File

@ -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 <https://flask.palletsprojects.com/en/1.1.x/>`_
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 <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/flask-blueprints>`_.
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 <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/flask-blueprints>`_.
.. disqus::

View File

@ -15,6 +15,7 @@ Explore the examples to see the ``Dependency Injector`` in action.
decoupled-packages decoupled-packages
django django
flask flask
flask-blueprints
aiohttp aiohttp
sanic sanic
fastapi fastapi

View File

@ -283,6 +283,7 @@ Choose one of the following as a next step:
- :ref:`decoupled-packages` - :ref:`decoupled-packages`
- :ref:`django-example` - :ref:`django-example`
- :ref:`flask-example` - :ref:`flask-example`
- :ref:`flask-blueprints-example`
- :ref:`aiohttp-example` - :ref:`aiohttp-example`
- :ref:`sanic-example` - :ref:`sanic-example`
- :ref:`fastapi-example` - :ref:`fastapi-example`

View File

@ -207,6 +207,7 @@ Take a look at other application examples:
- :ref:`decoupled-packages` - :ref:`decoupled-packages`
- :ref:`django-example` - :ref:`django-example`
- :ref:`flask-example` - :ref:`flask-example`
- :ref:`flask-blueprints-example`
- :ref:`aiohttp-example` - :ref:`aiohttp-example`
- :ref:`sanic-example` - :ref:`sanic-example`
- :ref:`fastapi-example` - :ref:`fastapi-example`