packages.rst
This file
Path
docs/patterns/packages.rst
133 lines · 4.0 KiB
Last changed by
Merge branch '2.3.x'David Lordbefore adoption15a0d4a
History of this fileTake 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:`pyproject.toml` next to the inner46:file:`yourapplication` folder with the following contents:4748.. code-block:: toml4950 [project]51 name = "yourapplication"52 dependencies = [53 "flask",54 ]5556 [build-system]57 requires = ["flit_core<4"]58 build-backend = "flit_core.buildapi"5960Install your application so it is importable:6162.. code-block:: text6364 $ pip install -e .6566To use the ``flask`` command and run your application you need to set67the ``--app`` option that tells Flask where to find the application68instance:6970.. code-block:: text7172 $ flask --app yourapplication run7374What did we gain from this? Now we can restructure the application a bit75into multiple modules. The only thing you have to remember is the76following quick checklist:77781. the `Flask` application object creation has to be in the79 :file:`__init__.py` file. That way each module can import it safely and the80 `__name__` variable will resolve to the correct package.812. all the view functions (the ones with a :meth:`~flask.Flask.route`82 decorator on top) have to be imported in the :file:`__init__.py` file.83 Not the object itself, but the module it is in. Import the view module84 **after the application object is created**.8586Here's an example :file:`__init__.py`::8788 from flask import Flask89 app = Flask(__name__)9091 import yourapplication.views9293And this is what :file:`views.py` would look like::9495 from yourapplication import app9697 @app.route('/')98 def index():99 return 'Hello World!'100101You should then end up with something like that::102103 /yourapplication104 pyproject.toml105 /yourapplication106 __init__.py107 views.py108 /static109 style.css110 /templates111 layout.html112 index.html113 login.html114 ...115116.. admonition:: Circular Imports117118 Every Python programmer hates them, and yet we just added some:119 circular imports (That's when two modules depend on each other. In this120 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a121 bad idea in general but here it is actually fine. The reason for this is122 that we are not actually using the views in :file:`__init__.py` and just123 ensuring the module is imported and we are doing that at the bottom of124 the file.125126127Working with Blueprints128-----------------------129130If you have larger applications it's recommended to divide them into131smaller groups where each group is implemented with the help of a132blueprint. For a gentle introduction into this topic refer to the133:doc:`/blueprints` chapter of the documentation.