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
The walk stopped after 200 commits: lines older than 41aaaf7 point at it.
clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
lines 1–44why, one step back1Large 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 notMerge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
lines 45–46why, one step back45a big problem, just add a new file called :file:`pyproject.toml` next to the inner46:file:`yourapplication` folder with the following contents:clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 47why, one step back47Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
line 48why, one step back48.. code-block:: tomlclean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 49why, one step back49Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
lines 50–54why, one step back50 [project]51 name = "yourapplication"52 dependencies = [53 "flask",54 ]clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 55why, one step back55Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
line 56why, one step back56 [build-system]Merge branch '2.3.x'David Lordbefore adoption15a0d4a
lines 57–58why, one step back57 requires = ["flit_core<4"]58 build-backend = "flit_core.buildapi"Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
line 59why, one step back59Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 60why, one step back60Install your application so it is importable:clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 61why, one step back61Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 62why, one step back62.. code-block:: textclean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 63why, one step back63Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 64why, one step back64 $ pip install -e .clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 65why, one step back65Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
lines 66–68why, one step back66To use the ``flask`` command and run your application you need to set67the ``--app`` option that tells Flask where to find the application68instance:clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
line 69why, one step back69Merge branch '2.2.x'David Lordbefore adoptiona18ae3d
lines 70–71why, one step back70.. code-block:: text71Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 72why, one step back72 $ flask --app yourapplication runclean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
lines 73–103why, one step back7374What 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 /yourapplicationMerge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
line 104why, one step back104 pyproject.tomlclean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7
lines 105–133why, one step back105 /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.