· 6 years ago · Apr 02, 2019, 09:26 PM
1import os
2
3from flask import Flask
4
5def create_app(test_config=None):
6 # create and configure the app object
7 # __name__ is the name of the current Python module
8 app = Flask(__name__, instance_relative_config=True)
9 app.config.from_mapping(
10 SECRET_KEY='dev', # override with a random value when deploying
11 DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
12 )
13
14 if test_config is None:
15 # load the instance config, if it exists, when not testing
16 # this is a good place to set a real SECRET_KEY
17 app.config.from_pyfile('config.py', silent=True)
18 else:
19 # load the test config, if it was passed in
20 app.config.from_mapping(test_config)
21
22 # the instance folder should exist
23 try:
24 os.makedirs(app.instance_path)
25 except OSError:
26 pass
27
28 # and now a page that says hello
29 @app.route('/hello')
30 def hello():
31 return "Hello, World!"
32
33 # configure the database
34 from . import db
35 db.init_app(app)
36
37 # having configured the app object, return it from the factory function
38 return app