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 fileTake it
1.. _larger-applications:23Larger Applications4===================56Imagine a simple flask application structure that looks like this::78 /yourapplication9 yourapplication.py10 /static11 style.css12 /templates13 layout.html14 index.html15 login.html16 ...1718While this is fine for small applications, for larger applications19it'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>`.2223Simple 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 )6061In order to run the application you need to export an environment variable62that tells Flask where to find the application instance::6364 $ export FLASK_APP=yourapplication6566If you are outside of the project directory make sure to provide the exact67path to your application directory. Similarly you can turn on the68development features like this::6970 $ export FLASK_ENV=development7172In order to install and run the application you need to issue the following73commands::7475 $ pip install -e .76 $ 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.