Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

143 lines · 4.5 KiB

Last changed by

Merge branch '1.0-maintenance'David Lordbefore adoptionac6746d

History of this file

Take it

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

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 1–5why, one step back
1.. _larger-applications:
23Larger Applications4===================5

Merge branch '1.0-maintenance'David Lordbefore adoptionac6746d

line 6why, one step back
6Imagine a simple flask application structure that looks like this::

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 7–17why, one step back
7
8 /yourapplication9 yourapplication.py10 /static11 style.css12 /templates13 layout.html14 index.html15 login.html16 ...17

Merge branch '1.0-maintenance'David Lordbefore adoptionac6746d

lines 18–21why, one step back
18While this is fine for small applications, for larger applications
19it's a good idea to use a package instead of a module.20The :ref:`tutorial <tutorial>` is structured to use the package pattern,21see the :gh:`example code <examples/tutorial>`.

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 22–60why, one step back
22
23Simple Packages24---------------2526To convert that into a larger one, just create a new folder27:file:`yourapplication` inside the existing one and move everything below it.28Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete29all ``.pyc`` files first, otherwise things would most likely break)3031You should then end up with something like that::3233 /yourapplication34 /yourapplication35 __init__.py36 /static37 style.css38 /templates39 layout.html40 index.html41 login.html42 ...4344But how do you run your application now? The naive ``python45yourapplication/__init__.py`` will not work. Let's just say that Python46does not want modules in packages to be the startup file. But that is not47a big problem, just add a new file called :file:`setup.py` next to the inner48:file:`yourapplication` folder with the following contents::4950 from setuptools import setup5152 setup(53 name='yourapplication',54 packages=['yourapplication'],55 include_package_data=True,56 install_requires=[57 'flask',58 ],59 )60

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 61–62why, one step back
61In order to run the application you need to export an environment variable
62that tells Flask where to find the application instance::

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 63why, one step back
63

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 64why, one step back
64 $ export FLASK_APP=yourapplication

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 65why, one step back
65

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 66why, one step back
66If you are outside of the project directory make sure to provide the exact

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 67–68why, one step back
67path to your application directory. Similarly you can turn on the
68development features like this::

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 69why, one step back
69

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 70why, one step back
70 $ export FLASK_ENV=development

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 71why, one step back
71

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 72why, one step back
72In order to install and run the application you need to issue the following

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 73–74why, one step back
73commands::
74

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

lines 75–76why, one step back
75 $ pip install -e .
76 $ flask run

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 77–143why, one step back
77
78What did we gain from this? Now we can restructure the application a bit79into multiple modules. The only thing you have to remember is the80following quick checklist:81821. the `Flask` application object creation has to be in the83 :file:`__init__.py` file. That way each module can import it safely and the84 `__name__` variable will resolve to the correct package.852. all the view functions (the ones with a :meth:`~flask.Flask.route`86 decorator on top) have to be imported in the :file:`__init__.py` file.87 Not the object itself, but the module it is in. Import the view module88 **after the application object is created**.8990Here's an example :file:`__init__.py`::9192 from flask import Flask93 app = Flask(__name__)9495 import yourapplication.views9697And this is what :file:`views.py` would look like::9899 from yourapplication import app100101 @app.route('/')102 def index():103 return 'Hello World!'104105You should then end up with something like that::106107 /yourapplication108 setup.py109 /yourapplication110 __init__.py111 views.py112 /static113 style.css114 /templates115 layout.html116 index.html117 login.html118 ...119120.. admonition:: Circular Imports121122 Every Python programmer hates them, and yet we just added some:123 circular imports (That's when two modules depend on each other. In this124 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a125 bad idea in general but here it is actually fine. The reason for this is126 that we are not actually using the views in :file:`__init__.py` and just127 ensuring the module is imported and we are doing that at the bottom of128 the file.129130 There are still some problems with that approach but if you want to use131 decorators there is no way around that. Check out the132 :ref:`becomingbig` section for some inspiration how to deal with that.133134135.. _working-with-modules:136137Working with Blueprints138-----------------------139140If you have larger applications it's recommended to divide them into141smaller groups where each group is implemented with the help of a142blueprint. For a gentle introduction into this topic refer to the143:ref:`blueprints` chapter of the documentation.