packages.rst
This file
Path
docs/patterns/packages.rst
115 lines · 3.7 KiB
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 86e8267 point at it.
Merge pull request #1378 from cbron/masterKeyan Pishdadianbefore adoption86e8267
lines 1–115why, 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: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)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 :file:`runserver.py` next to the inner45:file:`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 :file:`__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 in the :file:`__init__.py` file.59 Not the object itself, but the module it is in. Import the view module60 **after the application object is created**.6162Here's an example :file:`__init__.py`::6364 from flask import Flask65 app = Flask(__name__)6667 import yourapplication.views6869And this is what :file:`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 /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 this96 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a97 bad idea in general but here it is actually fine. The reason for this is98 that we are not actually using the views in :file:`__init__.py` and just99 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.