packages.rst
This file
Path
docs/patterns/packages.rst
114 lines · 3.6 KiB
Last changed by
Removed old module documentation and added some new for blueprints.Armin Ronacherbefore adoptiondbfa04e
History of this fileTake it
The walk stopped after 200 commits: lines older than 91e9632 point at it.
Moved wsgi_app down to a more logical locationArmin Ronacherbefore adoption91e9632
lines 1–58why, 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 folder24`yourapplication` inside the existing one and move everything below it.25Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete26all `.pyc` files first, otherwise things would most likely break)2728You 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 not44a big problem, just add a new file called `runserver.py` next to the inner45`yourapplication` folder with the following contents::4647 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 the55 `__init__.py` file. That way each module can import it safely and the56 `__name__` variable will resolve to the correct package.572. all the view functions (the ones with a :meth:`~flask.Flask.route`58 decorator on top) have to be imported when in the `__init__.py` file.Clarified language related to avoiding circular imports.Drew Vogelbefore adoption3deae1b
line 59why, one step back59 Not the object itself, but the module it is in. Import the view moduleMerge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18
line 60why, one step back60 **after the application object is created**.Moved wsgi_app down to a more logical locationArmin Ronacherbefore adoption91e9632
lines 61–107why, one step back6162Here's an example `__init__.py`::6364 from flask import Flask65 app = Flask(__name__)6667 import yourapplication.views6869And this is what `views.py` would look like::7071 from yourapplication import app7273 @app.route('/')74 def index():75 return 'Hello World!'7677You should then end up with something like that::7879 /yourapplication80 /yourapplication81 /__init__.py82 /views.py83 /static84 /style.css85 /templates86 layout.html87 index.html88 login.html89 ...9091.. admonition:: Circular Imports9293 Every Python programmer hates them, and yet we just added some:94 circular imports (That's when two modules depend on each other. In this95 case `views.py` depends on `__init__.py`). Be advised that this is a96 bad idea in general but here it is actually fine. The reason for this is97 that we are not actually using the views in `__init__.py` and just98 ensuring the module is imported and we are doing that at the bottom of99 the file.100101 There are still some problems with that approach but if you want to use102 decorators there is no way around that. Check out the103 :ref:`becomingbig` section for some inspiration how to deal with that.104105106.. _working-with-modules:107Removed old module documentation and added some new for blueprints.Armin Ronacherbefore adoptiondbfa04e
lines 108–109why, one step back108Working with Blueprints109-----------------------Moved wsgi_app down to a more logical locationArmin Ronacherbefore adoption91e9632
line 110why, one step back110Removed old module documentation and added some new for blueprints.Armin Ronacherbefore adoptiondbfa04e
lines 111–114why, one step back111If you have larger applications it's recommended to divide them into112smaller groups where each group is implemented with the help of a113blueprint. For a gentle introduction into this topic refer to the114:ref:`blueprints` chapter of the documentation.