packages.rst
This file
Path
docs/patterns/packages.rst
143 lines · 4.4 KiB
Last changed by
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
History of this fileTake it
The walk stopped after 200 commits: lines older than d2a46dc point at it.
fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 1–19why, 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 ...19Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 20–21why, one step back20The :ref:`tutorial <tutorial>` is structured this way, see the21:gh:`example code <examples/tutorial>`.fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 22–60why, one step back2223Simple 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 )60Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 61–62why, one step back61In order to run the application you need to export an environment variable62that tells Flask where to find the application instance::fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 63–65why, one step back6364 export FLASK_APP=yourapplication65Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 66why, one step back66If you are outside of the project directory make sure to provide the exactAdd Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 67–68why, one step back67path to your application directory. Similarly you can turn on the68development features like this::fix doc build errorDavid Lordbefore adoptiond2a46dc
line 69why, one step back69Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
line 70why, one step back70 export FLASK_ENV=developmentfix doc build errorDavid Lordbefore adoptiond2a46dc
line 71why, one step back71Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 72why, one step back72In order to install and run the application you need to issue the followingfix doc build errorDavid Lordbefore adoptiond2a46dc
lines 73–74why, one step back73commands::74Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 75why, one step back75 pip install -e .fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 76–143why, one step back76 flask run7778What 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.