Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

86 lines · 2.9 KiB

Last changed by

Fix typos and remove unused import.florentxbefore adoption3c821a0

History of this file

Take it

Breaking docs up into smaller piecesArmin Ronacherbefore adoption123a1ad

lines 1–50why, 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 /yourapplication11 /yourapplication.py12 /static13 /style.css14 /templates15 layout.html16 index.html17 login.html18 ...1920To convert that into a larger one, just create a new folder21`yourapplication` inside the existing one and move everything below it.22Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete23all `.pyc` files first, otherwise things would most likely break)2425You should then end up with something like that::2627 /yourapplication28 /yourapplication29 /__init__.py30 /static31 /style.css32 /templates33 layout.html34 index.html35 login.html36 ...3738But how do you run your application now? The naive ``python39yourapplication/__init__.py`` will not work. Let's just say that Python40does not want modules in packages to be the startup file. But that is not41a big problem, just add a new file called `runserver.py` next to the inner42`yourapplication` folder with the following contents::4344 from yourapplication import app45 app.run(debug=True)4647What did we gain from this? Now we can restructure the application a bit48into multiple modules. The only thing you have to remember is the49following quick checklist:50

Proofreading the documentationChris Edgemonbefore adoptiona7ff9db

line 51why, one step back
511. the `Flask` application object creation has to be in the

Breaking docs up into smaller piecesArmin Ronacherbefore adoption123a1ad

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

Proofreading the documentationChris Edgemonbefore adoptiona7ff9db

line 53why, one step back
53 `__name__` variable will resolve to the correct package.

Breaking docs up into smaller piecesArmin Ronacherbefore adoption123a1ad

lines 54–55why, one step back
542. all the view functions (the ones with a :meth:`~flask.Flask.route`
55 decorator on top) have to be imported when in the `__init__.py` file.

Proofreading the documentationChris Edgemonbefore adoptiona7ff9db

line 56why, one step back
56 Not the object itself, but the module it is in. Do the importing at

Breaking docs up into smaller piecesArmin Ronacherbefore adoption123a1ad

lines 57–76why, one step back
57 the *bottom* of the file.
5859Here an example `__init__.py`::6061 from flask import Flask62 app = Flask(__name__)6364 import yourapplication.views6566And this is what `views.py` would look like::6768 from yourapplication import app6970 @app.route('/')71 def index():72 return 'Hello World!'7374.. admonition:: Circular Imports7576 Every Python programmer hates them, and yet we just added some:

Fix typos and remove unused import.florentxbefore adoption3c821a0

line 77why, one step back
77 circular imports (That's when two modules depend on each other. In this

Breaking docs up into smaller piecesArmin Ronacherbefore adoption123a1ad

line 78why, one step back
78 case `views.py` depends on `__init__.py`). Be advised that this is a

Fix typos and remove unused import.florentxbefore adoption3c821a0

line 79why, one step back
79 bad idea in general but here it is actually fine. The reason for this is

Breaking docs up into smaller piecesArmin Ronacherbefore adoption123a1ad

lines 80–86why, one step back
80 that we are not actually using the views in `__init__.py` and just
81 ensuring the module is imported and we are doing that at the bottom of82 the file.8384 There are still some problems with that approach but if you want to use85 decorators there is no way around that. Check out the86 :ref:`becomingbig` section for some inspiration how to deal with that.