Commit ff4449691cd3590db4b91cceaebf93b0711d32ea
1 parent
1b39d5ec
Exists in
master
Add the app factory.
Showing
1 changed file
with
83 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,83 @@ |
1 | +#! ../venv/bin/python | |
2 | + | |
3 | +from flask import Flask | |
4 | +from webassets.loaders import PythonLoader as PythonAssetsLoader | |
5 | + | |
6 | +from flaskr import assets | |
7 | +from flaskr.models import db | |
8 | +from flaskr.controllers.main import main | |
9 | + | |
10 | +from flaskr.extensions import ( | |
11 | + cache, | |
12 | + assets_env, | |
13 | + debug_toolbar, | |
14 | + login_manager | |
15 | +) | |
16 | + | |
17 | +from yaml import safe_load as yaml_safe_load | |
18 | + | |
19 | +from markdown import markdown | |
20 | + | |
21 | + | |
22 | +def create_app(object_name): | |
23 | + """ | |
24 | + An flask application factory, as explained here: | |
25 | + http://flask.pocoo.org/docs/patterns/appfactories/ | |
26 | + | |
27 | + Arguments: | |
28 | + object_name: the python path of the config object, | |
29 | + e.g. flaskr.settings.ProductionConfig | |
30 | + """ | |
31 | + | |
32 | + app = Flask(__name__) | |
33 | + | |
34 | + # We bypass object_name (for now) | |
35 | + object_name = 'flaskr.settings.DevelopmentConfig' | |
36 | + | |
37 | + # Load configuration | |
38 | + app.config.from_object(object_name) | |
39 | + | |
40 | + # initialize the cache | |
41 | + cache.init_app(app) | |
42 | + | |
43 | + # initialize the debug tool bar | |
44 | + debug_toolbar.init_app(app) | |
45 | + | |
46 | + # initialize SQLAlchemy | |
47 | + db.init_app(app) | |
48 | + | |
49 | + login_manager.init_app(app) | |
50 | + | |
51 | + # Import and register the different asset bundles | |
52 | + assets_env.init_app(app) | |
53 | + assets_loader = PythonAssetsLoader(assets) | |
54 | + for name, bundle in assets_loader.load_bundles().items(): | |
55 | + assets_env.register(name, bundle) | |
56 | + | |
57 | + # register our blueprints | |
58 | + app.register_blueprint(main) | |
59 | + | |
60 | + # Load content data from the YAML file | |
61 | + content = {} | |
62 | + with open('content.yml', 'r') as content_file: | |
63 | + content = yaml_safe_load(content_file.read()) | |
64 | + | |
65 | + # VERSION | |
66 | + version = "0.0.0" | |
67 | + with open('VERSION', 'r') as version_file: | |
68 | + version = version_file.read().strip() | |
69 | + | |
70 | + # Inject it as global template var | |
71 | + @app.context_processor | |
72 | + def inject_template_global_variables(): | |
73 | + return dict( | |
74 | + content=content, | |
75 | + version=version, | |
76 | + ) | |
77 | + | |
78 | + # Markdown jinja2 filter | |
79 | + @app.template_filter('markdown') | |
80 | + def markdown_filter(text): | |
81 | + return markdown(text) | |
82 | + | |
83 | + return app | ... | ... |