Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Create your app with a function

Posted by Dan Jacob on 2010-05-25 @ 09:05 and filed in Application Structure

A useful pattern is creating your application using a function. This allows you to pass in different configuration settings, for example in unit tests.

For example:

from flask import Flask
from sqlalchemy import create_engine

from myapp import config
from myapp.views import frontend

def create_app(database_uri, debug=False):
    app = Flask(__name__)
    app.debug = debug

    # set up your database
    app.engine = create_engine(database_uri)

    # add your modules
    app.register_module(frontend)
    
    # other setup tasks

    return app

Then you can do this:

if __name__ == "__main__":
    app = create_app(config.DATABASE_URI, debug=True)
    app.run()

or in your unit tests:

import unittest

from myapp import config
from myapp import create_app

class TestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app(config.TEST_DATABASE_URI)
        self.client = self.app.test_client()

This snippet by Dan Jacob can be used freely for anything you like. Consider it public domain.

Comments