Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 36d70d8 point at it.

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 1–23why, one step back
1.. _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 folder

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 24–26why, one step back
24: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 pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 27–43why, one step back
27
28You 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 not

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 44–45why, one step back
44a big problem, just add a new file called :file:`runserver.py` next to the inner
45:file:`yourapplication` folder with the following contents::

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 46–54why, one step back
46
47 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 the

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 55why, one step back
55 :file:`__init__.py` file. That way each module can import it safely and the

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 56–57why, one step back
56 `__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 back
58 decorator on top) have to be imported in the :file:`__init__.py` file.

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 59–61why, one step back
59 Not the object itself, but the module it is in. Import the view module
60 **after the application object is created**.61

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 62why, one step back
62Here's an example :file:`__init__.py`::

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 63–68why, one step back
63
64 from flask import Flask65 app = Flask(__name__)6667 import yourapplication.views68

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 69why, one step back
69And this is what :file:`views.py` would look like::

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 70–95why, one step back
70
71 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 this

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 96why, one step back
96 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

line 97why, one step back
97 bad idea in general but here it is actually fine. The reason for this is

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 98why, one step back
98 that we are not actually using the views in :file:`__init__.py` and just

Merge pull request #843 from wbowlin/masterDaniel Neuhäuserbefore adoption36d70d8

lines 99–115why, one step back
99 ensuring the module is imported and we are doing that at the bottom of
100 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.