Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

130 lines · 4.0 KiB

Last changed by

Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727

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    $ flask --app yourapplication run7071What did we gain from this?  Now we can restructure the application a bit72into multiple modules.  The only thing you have to remember is the73following quick checklist:74751. the `Flask` application object creation has to be in the76   :file:`__init__.py` file.  That way each module can import it safely and the77   `__name__` variable will resolve to the correct package.782. all the view functions (the ones with a :meth:`~flask.Flask.route`79   decorator on top) have to be imported in the :file:`__init__.py` file.80   Not the object itself, but the module it is in. Import the view module81   **after the application object is created**.8283Here's an example :file:`__init__.py`::8485    from flask import Flask86    app = Flask(__name__)8788    import yourapplication.views8990And this is what :file:`views.py` would look like::9192    from yourapplication import app9394    @app.route('/')95    def index():96        return 'Hello World!'9798You should then end up with something like that::99100    /yourapplication101        setup.py102        /yourapplication103            __init__.py104            views.py105            /static106                style.css107            /templates108                layout.html109                index.html110                login.html111                ...112113.. admonition:: Circular Imports114115   Every Python programmer hates them, and yet we just added some:116   circular imports (That's when two modules depend on each other.  In this117   case :file:`views.py` depends on :file:`__init__.py`).  Be advised that this is a118   bad idea in general but here it is actually fine.  The reason for this is119   that we are not actually using the views in :file:`__init__.py` and just120   ensuring the module is imported and we are doing that at the bottom of121   the file.122123124Working with Blueprints125-----------------------126127If you have larger applications it's recommended to divide them into128smaller groups where each group is implemented with the help of a129blueprint.  For a gentle introduction into this topic refer to the130:doc:`/blueprints` chapter of the documentation.