Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

132 lines · 4.0 KiB

Last changed by

Merge pull request #4966 from ivanovmg/fix/cmd-formatDavid Lordbefore adoption761e02e

History of this file

Take it

1Large Applications as Packages2==============================34Imagine a simple flask application structure that looks like this::56    /yourapplication7        yourapplication.py8        /static9            style.css10        /templates11            layout.html12            index.html13            login.html14            ...1516While this is fine for small applications, for larger applications17it's a good idea to use a package instead of a module.18The :doc:`/tutorial/index` is structured to use the package pattern,19see the :gh:`example code <examples/tutorial>`.2021Simple Packages22---------------2324To convert that into a larger one, just create a new folder25:file:`yourapplication` inside the existing one and move everything below it.26Then rename :file:`yourapplication.py` to :file:`__init__.py`.  (Make sure to delete27all ``.pyc`` files first, otherwise things would most likely break)2829You should then end up with something like that::3031    /yourapplication32        /yourapplication33            __init__.py34            /static35                style.css36            /templates37                layout.html38                index.html39                login.html40                ...4142But how do you run your application now?  The naive ``python43yourapplication/__init__.py`` will not work.  Let's just say that Python44does not want modules in packages to be the startup file.  But that is not45a big problem, just add a new file called :file:`setup.py` next to the inner46:file:`yourapplication` folder with the following contents::4748    from setuptools import setup4950    setup(51        name='yourapplication',52        packages=['yourapplication'],53        include_package_data=True,54        install_requires=[55            'flask',56        ],57    )5859Install your application so it is importable:6061.. code-block:: text6263    $ pip install -e .6465To use the ``flask`` command and run your application you need to set66the ``--app`` option that tells Flask where to find the application67instance:6869.. code-block:: text7071    $ flask --app yourapplication run7273What did we gain from this?  Now we can restructure the application a bit74into multiple modules.  The only thing you have to remember is the75following quick checklist:76771. the `Flask` application object creation has to be in the78   :file:`__init__.py` file.  That way each module can import it safely and the79   `__name__` variable will resolve to the correct package.802. all the view functions (the ones with a :meth:`~flask.Flask.route`81   decorator on top) have to be imported in the :file:`__init__.py` file.82   Not the object itself, but the module it is in. Import the view module83   **after the application object is created**.8485Here's an example :file:`__init__.py`::8687    from flask import Flask88    app = Flask(__name__)8990    import yourapplication.views9192And this is what :file:`views.py` would look like::9394    from yourapplication import app9596    @app.route('/')97    def index():98        return 'Hello World!'99100You should then end up with something like that::101102    /yourapplication103        setup.py104        /yourapplication105            __init__.py106            views.py107            /static108                style.css109            /templates110                layout.html111                index.html112                login.html113                ...114115.. admonition:: Circular Imports116117   Every Python programmer hates them, and yet we just added some:118   circular imports (That's when two modules depend on each other.  In this119   case :file:`views.py` depends on :file:`__init__.py`).  Be advised that this is a120   bad idea in general but here it is actually fine.  The reason for this is121   that we are not actually using the views in :file:`__init__.py` and just122   ensuring the module is imported and we are doing that at the bottom of123   the file.124125126Working with Blueprints127-----------------------128129If you have larger applications it's recommended to divide them into130smaller groups where each group is implemented with the help of a131blueprint.  For a gentle introduction into this topic refer to the132:doc:`/blueprints` chapter of the documentation.