Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

145 lines · 4.6 KiB

Last changed by

resolve merge conflictsKenneth Reitzbefore adoption31174fe

History of this file

Take it

The walk stopped after 200 commits: lines older than 7550f43 point at it.

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 1–10why, 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 /yourapplication

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 11why, one step back
11 yourapplication.py

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 12why, one step back
12 /static

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 13why, one step back
13 style.css

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 14–19why, one step back
14 /templates
15 layout.html16 index.html17 login.html18 ...19

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 20–23why, one step back
20If you find yourself stuck on something, feel free
21to take a look at the source code for this example.22You'll find `the full src for this example here`_.23

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 24–27why, one step back
24Simple Packages
25---------------2627To convert that into a larger one, just create a new folder

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

lines 28–30why, one step back
28:file:`yourapplication` inside the existing one and move everything below it.
29Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete30all ``.pyc`` files first, otherwise things would most likely break)

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 31–35why, one step back
31
32You should then end up with something like that::3334 /yourapplication35 /yourapplication

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 36why, one step back
36 __init__.py

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 37why, one step back
37 /static

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 38why, one step back
38 style.css

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 39–47why, one step back
39 /templates
40 layout.html41 index.html42 login.html43 ...4445But how do you run your application now? The naive ``python46yourapplication/__init__.py`` will not work. Let's just say that Python47does not want modules in packages to be the startup file. But that is not

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 48why, one step back
48a big problem, just add a new file called :file:`setup.py` next to the inner

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

line 49why, one step back
49:file:`yourapplication` folder with the following contents::

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 50why, one step back
50

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 51why, one step back
51 from setuptools import setup

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 52why, one step back
52

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 53–78why, one step back
53 setup(
54 name='yourapplication',55 packages=['yourapplication'],56 include_package_data=True,57 install_requires=[58 'flask',59 ],60 )6162In order to run the application you need to export an environment variable 63that tells Flask where to find the application instance:: 6465 export FLASK_APP=yourapplication6667If you are outside of the project directory make sure to provide the exact 68path to your application directory. Similarly you can turn on "debug 69mode" with this environment variable:: 7071 export FLASK_DEBUG=true 7273In order to install and run the application you need to issue the following 74commands::7576 pip install -e . 77 flask run78

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 79–83why, one step back
79What did we gain from this? Now we can restructure the application a bit
80into multiple modules. The only thing you have to remember is the81following quick checklist:82831. the `Flask` application object creation has to be in the

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

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 85–86why, one step back
85 `__name__` variable will resolve to the correct package.
862. 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 87why, one step back
87 decorator on top) have to be imported in the :file:`__init__.py` file.

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 88–90why, one step back
88 Not the object itself, but the module it is in. Import the view module
89 **after the application object is created**.90

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

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 92–97why, one step back
92
93 from flask import Flask94 app = Flask(__name__)9596 import yourapplication.views97

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

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 99–108why, one step back
99
100 from yourapplication import app101102 @app.route('/')103 def index():104 return 'Hello World!'105106You should then end up with something like that::107108 /yourapplication

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 109why, one step back
109 setup.py

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 110why, one step back
110 /yourapplication

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 111–112why, one step back
111 __init__.py
112 views.py

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 113why, one step back
113 /static

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 114why, one step back
114 style.css

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 115–124why, one step back
115 /templates
116 layout.html117 index.html118 login.html119 ...120121.. admonition:: Circular Imports122123 Every Python programmer hates them, and yet we just added some:124 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 125why, one step back
125 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 126why, one step back
126 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 127why, one step back
127 that we are not actually using the views in :file:`__init__.py` and just

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 128–136why, one step back
128 ensuring the module is imported and we are doing that at the bottom of
129 the file.130131 There are still some problems with that approach but if you want to use132 decorators there is no way around that. Check out the133 :ref:`becomingbig` section for some inspiration how to deal with that.134135136.. _working-with-modules:

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 137why, one step back
137.. _the full src for this example here: https://github.com/pallets/flask/tree/master/examples/patterns/largerapp

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

lines 138–145why, one step back
138
139Working with Blueprints140-----------------------141142If you have larger applications it's recommended to divide them into143smaller groups where each group is implemented with the help of a144blueprint. For a gentle introduction into this topic refer to the145:ref:`blueprints` chapter of the documentation.