Larger Applications

Imagine a simple flask application structure that looks like this:

  1. /yourapplication
  2. yourapplication.py
  3. /static
  4. style.css
  5. /templates
  6. layout.html
  7. index.html
  8. login.html
  9. ...

While this is fine for small applications, for larger applicationsit’s a good idea to use a package instead of a module.The tutorial is structured to use the package pattern,see the example code.

Simple Packages

To convert that into a larger one, just create a new folderyourapplication inside the existing one and move everything below it.Then rename yourapplication.py to init.py. (Make sure to deleteall .pyc files first, otherwise things would most likely break)

You should then end up with something like that:

  1. /yourapplication
  2. /yourapplication
  3. __init__.py
  4. /static
  5. style.css
  6. /templates
  7. layout.html
  8. index.html
  9. login.html
  10. ...

But how do you run your application now? The naive pythonyourapplication/init.py will not work. Let’s just say that Pythondoes not want modules in packages to be the startup file. But that is nota big problem, just add a new file called setup.py next to the inneryourapplication folder with the following contents:

  1. from setuptools import setup
  2.  
  3. setup(
  4. name='yourapplication',
  5. packages=['yourapplication'],
  6. include_package_data=True,
  7. install_requires=[
  8. 'flask',
  9. ],
  10. )

In order to run the application you need to export an environment variablethat tells Flask where to find the application instance:

  1. $ export FLASK_APP=yourapplication

If you are outside of the project directory make sure to provide the exactpath to your application directory. Similarly you can turn on thedevelopment features like this:

  1. $ export FLASK_ENV=development

In order to install and run the application you need to issue the followingcommands:

  1. $ pip install -e .
  2. $ flask run

What did we gain from this? Now we can restructure the application a bitinto multiple modules. The only thing you have to remember is thefollowing quick checklist:

  • the Flask application object creation has to be in theinit.py file. That way each module can import it safely and thename variable will resolve to the correct package.

  • all the view functions (the ones with a route()decorator on top) have to be imported in the init.py file.Not the object itself, but the module it is in. Import the view moduleafter the application object is created.

Here’s an example init.py:

  1. from flask import Flask
  2. app = Flask(__name__)
  3.  
  4. import yourapplication.views

And this is what views.py would look like:

  1. from yourapplication import app
  2.  
  3. @app.route('/')
  4. def index():
  5. return 'Hello World!'

You should then end up with something like that:

  1. /yourapplication
  2. setup.py
  3. /yourapplication
  4. __init__.py
  5. views.py
  6. /static
  7. style.css
  8. /templates
  9. layout.html
  10. index.html
  11. login.html
  12. ...

Circular Imports

Every Python programmer hates them, and yet we just added some:circular imports (That’s when two modules depend on each other. In thiscase views.py depends on init.py). Be advised that this is abad idea in general but here it is actually fine. The reason for this isthat we are not actually using the views in init.py and justensuring the module is imported and we are doing that at the bottom ofthe file.

There are still some problems with that approach but if you want to usedecorators there is no way around that. Check out theBecoming Big section for some inspiration how to deal with that.

Working with Blueprints

If you have larger applications it’s recommended to divide them intosmaller groups where each group is implemented with the help of ablueprint. For a gentle introduction into this topic refer to theModular Applications with Blueprints chapter of the documentation.