-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.5
kwmccabe edited this page Apr 17, 2018
·
9 revisions
v0.5 - AppConfig: DevelopmentConfig, TestingConfig, ProductionConfig, ENV FLASK_CONFIG
- +6 -1 [M] web/Dockerfile
- +4 -4 [M] web/app/init.py
- +25 -0 [M] web/config.py
- +4 -3 [M] web/flaskapp.py
- Update comments.
- Add
FLASK_CONFIG
environment variable.
# base container image
FROM python:3.6.1-alpine
-# add tzdata to base image; set timezone environment variable
+# update system packages
RUN apk add --no-cache tzdata
+
+# set environment variables for system
ENV TZ America/Los_Angeles
+# set environment variables for application
+ENV FLASK_CONFIG development
+
# setup working directory within container
RUN mkdir -p /web
WORKDIR /web
- Replace direct use of
AppConfig
with reference to a config subclassDevelopmentConfig
,TestingConfig
orProductionConfig
.
from flask import Flask
-from config import AppConfig
+from config import config
-def create_app():
+def create_app(config_name):
app = Flask(__name__)
- app.config.from_object(AppConfig)
- AppConfig.init_app(app)
+ app.config.from_object(config[config_name])
+ config[config_name].init_app(app)
return app
- Create subclasses of
AppConfig
corresponding to different deployment environments. -
DevelopmentConfig
andTestingConfig
contain single values for now. -
ProductionConfig
contains a placeholderinit_app()
method. - Create
config
dictionary to mapFLASK_CONFIG
values to objects.
@staticmethod
def init_app(app):
pass
+
+
+class DevelopmentConfig(AppConfig):
+ DEBUG = True
+
+class TestingConfig(AppConfig):
+ TESTING = True
+
+
+class ProductionConfig(AppConfig):
+ DEBUG = False
+ TESTING = False
+
+ @classmethod
+ def init_app(cls, app):
+ # multi-step setups could go here
+ AppConfig.init_app(app)
+
+
+config = {
+ 'development': DevelopmentConfig,
+ 'testing': TestingConfig,
+ 'production': ProductionConfig,
+ 'default': DevelopmentConfig
+}
- Pass the environment value
FLASK_CONFIG
through toapp.create_app()
. - Update the route
/info/config
to display the value ofFLASK_CONFIG
.
-app = create_app()
+app = create_app(os.getenv('FLASK_CONFIG') or 'default')
...
@app.route('/info/config')
def app_config():
cnf = dict(app.config)
- return "Current Config : %s" % cnf
+ return "'%s' Config : %s" % (os.getenv('FLASK_CONFIG'),cnf)
Commit-v0.4 | Commit-v0.5 | Commit-v0.6
- FlaskApp Tutorial
- Table of Contents
- About
- Application Setup
- Modules, Templates, and Layouts
- Database Items, Forms, and CRUD
- List Filter, Sort, and Paginate
- Users and Login
- Database Relationships
- API Module, HTTPAuth and JSON
- Refactoring User Roles and Item Status
- AJAX and Public Pages