Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

140 lines · 4.3 KiB

Last changed by

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

History of this file

Take it

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

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

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

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

line 11why, one step back
11 yourapplication.py

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 12why, one step back
12 /static

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

line 13why, one step back
13 style.css

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

lines 14–31why, one step back
14 /templates
15 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 /yourapplication

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

line 32why, one step back
32 __init__.py

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 33why, one step back
33 /static

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

line 34why, one step back
34 style.css

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

lines 35–43why, one step back
35 /templates
36 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 branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

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

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

lines 45–46why, one step back
45:file:`yourapplication` folder with the following contents::
46

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

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

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 48why, one step back
48

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

lines 49–74why, one step back
49 setup(
50 name='yourapplication',51 packages=['yourapplication'],52 include_package_data=True,53 install_requires=[54 'flask',55 ],56 )5758In order to run the application you need to export an environment variable 59that tells Flask where to find the application instance:: 6061 export FLASK_APP=yourapplication6263If you are outside of the project directory make sure to provide the exact 64path to your application directory. Similiarly you can turn on "debug 65mode" with this environment variable:: 6667 export FLASK_DEBUG=true 6869In order to install and run the application you need to issue the following 70commands::7172 pip install -e . 73 flask run74

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

lines 75–104why, one step back
75What did we gain from this? Now we can restructure the application a bit
76into multiple modules. The only thing you have to remember is the77following quick checklist:78791. the `Flask` application object creation has to be in the80 :file:`__init__.py` file. That way each module can import it safely and the81 `__name__` variable will resolve to the correct package.822. all the view functions (the ones with a :meth:`~flask.Flask.route`83 decorator on top) have to be imported in the :file:`__init__.py` file.84 Not the object itself, but the module it is in. Import the view module85 **after the application object is created**.8687Here's an example :file:`__init__.py`::8889 from flask import Flask90 app = Flask(__name__)9192 import yourapplication.views9394And this is what :file:`views.py` would look like::9596 from yourapplication import app9798 @app.route('/')99 def index():100 return 'Hello World!'101102You should then end up with something like that::103104 /yourapplication

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

line 105why, one step back
105 setup.py

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 106why, one step back
106 /yourapplication

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

lines 107–108why, one step back
107 __init__.py
108 views.py

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

line 109why, one step back
109 /static

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

line 110why, one step back
110 style.css

Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d

lines 111–140why, one step back
111 /templates
112 layout.html113 index.html114 login.html115 ...116117.. admonition:: Circular Imports118119 Every Python programmer hates them, and yet we just added some:120 circular imports (That's when two modules depend on each other. In this121 case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a122 bad idea in general but here it is actually fine. The reason for this is123 that we are not actually using the views in :file:`__init__.py` and just124 ensuring the module is imported and we are doing that at the bottom of125 the file.126127 There are still some problems with that approach but if you want to use128 decorators there is no way around that. Check out the129 :ref:`becomingbig` section for some inspiration how to deal with that.130131132.. _working-with-modules:133134Working with Blueprints135-----------------------136137If you have larger applications it's recommended to divide them into138smaller groups where each group is implemented with the help of a139blueprint. For a gentle introduction into this topic refer to the140:ref:`blueprints` chapter of the documentation.