packages.rst
This file
Path
docs/patterns/packages.rst
115 lines · 3.7 KiB
Last changed by
Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
History of this fileTake it
The walk stopped after 200 commits: lines older than 5693b9d point at it.
Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 1–23why, one step back1.. _larger-applications:23Larger Applications4===================56For larger applications it's a good idea to use a package instead of a7module. That is quite simple. Imagine a small application looks like8this::910 /yourapplication11 /yourapplication.py12 /static13 /style.css14 /templates15 layout.html16 index.html17 login.html18 ...1920Simple Packages21---------------2223To convert that into a larger one, just create a new folderMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
lines 24–26why, one step back24:file:`yourapplication` inside the existing one and move everything below it.25Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete26all ``.pyc`` files first, otherwise things would most likely break)Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 27–43why, one step back2728You should then end up with something like that::2930 /yourapplication31 /yourapplication32 /__init__.py33 /static34 /style.css35 /templates36 layout.html37 index.html38 login.html39 ...4041But how do you run your application now? The naive ``python42yourapplication/__init__.py`` will not work. Let's just say that Python43does not want modules in packages to be the startup file. But that is notMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
lines 44–45why, one step back44a big problem, just add a new file called :file:`runserver.py` next to the inner45:file:`yourapplication` folder with the following contents::Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 46–54why, one step back4647 from yourapplication import app48 app.run(debug=True)4950What did we gain from this? Now we can restructure the application a bit51into multiple modules. The only thing you have to remember is the52following quick checklist:53541. the `Flask` application object creation has to be in theMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 55why, one step back55 :file:`__init__.py` file. That way each module can import it safely and theMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 56–57why, one step back56 `__name__` variable will resolve to the correct package.572. all the view functions (the ones with a :meth:`~flask.Flask.route`Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 58why, one step back58 decorator on top) have to be imported in the :file:`__init__.py` file.Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 59–61why, one step back59 Not the object itself, but the module it is in. Import the view module60 **after the application object is created**.61Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 62why, one step back62Here's an example :file:`__init__.py`::Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 63–68why, one step back6364 from flask import Flask65 app = Flask(__name__)6667 import yourapplication.views68Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 69why, one step back69And this is what :file:`views.py` would look like::Merge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 70–95why, one step back7071 from yourapplication import app7273 @app.route('/')74 def index():75 return 'Hello World!'7677You should then end up with something like that::7879 /yourapplication80 /runserver.py81 /yourapplication82 /__init__.py83 /views.py84 /static85 /style.css86 /templates87 layout.html88 index.html89 login.html90 ...9192.. admonition:: Circular Imports9394 Every Python programmer hates them, and yet we just added some:95 circular imports (That's when two modules depend on each other. In thisMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 96why, one step back96 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is aMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
line 97why, one step back97 bad idea in general but here it is actually fine. The reason for this isMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 98why, one step back98 that we are not actually using the views in :file:`__init__.py` and justMerge branch 'pr/839'Armin Ronacherbefore adoption5693b9d
lines 99–115why, one step back99 ensuring the module is imported and we are doing that at the bottom of100 the file.101102 There are still some problems with that approach but if you want to use103 decorators there is no way around that. Check out the104 :ref:`becomingbig` section for some inspiration how to deal with that.105106107.. _working-with-modules:108109Working with Blueprints110-----------------------111112If you have larger applications it's recommended to divide them into113smaller groups where each group is implemented with the help of a114blueprint. For a gentle introduction into this topic refer to the115:ref:`blueprints` chapter of the documentation.