設定の詳細

New in version 0.3.

アプリケーションいくつかの設定が必要です。デバックモードの切り替えか、シークレットキーの 切り替えやその他似たよう細かい設定がたくさん有ります。

通常Flaskではアプリケーションを新規作成する際にいくつかの設定をします。 あなたは、設定を記述することも出来ますし、もっといい他の方法もあります。

独立したあなた用の設定ファイルを読み込ませることが出来ます。 Flask クラス内の config オブジェクトを 利用することで外部の設定値をロードすることが可能です。

基本設定

config のサブクラスを、辞書型を扱うように簡単に書き換えられます:

app = Flask(__name__)
app.config['DEBUG'] = True

以下のような記述も同様に動作します:

app.debug = True

複数のパラメータを一度に設定したい場合は dict.update() を 使用して記述します:

app.config.update(
    DEBUG=True,
    SECRET_KEY='...'
)

基本設定項目一覧

Flask内部で利用される設定項目:

DEBUG enable/disable debug mode
TESTING enable/disable testing mode
SECRET_KEY the secret key
SESSION_COOKIE_NAME the name of the session cookie
PERMANENT_SESSION_LIFETIME the lifetime of a permanent session as datetime.timedelta object.
USE_X_SENDFILE enable/disable x-sendfile
LOGGER_NAME the name of the logger
SERVER_NAME the name of the server. Required for subdomain support (eg: 'localhost')

New in version 0.4: LOGGER_NAME

New in version 0.5: SERVER_NAME

外部設定ファイル

設定ファイルを外部に作成するとより使いやすくなります。アプリケーションパッケージ外部から 参照することで、各アプリケーションでの設定の手間を減らすことが出来ます

So a common pattern is this:

app = Flask(__name__)
app.config.from_object('yourapplication.default_settings')
app.config.from_envvar('YOURAPPLICATION_SETTINGS')

This first loads the configuration from the yourapplication.default_settings module and then overrides the values with the contents of the file the YOURAPPLICATION_SETTINGS environment variable points to. This environment variable can be set on Linux or OS X with the export command in the shell before starting the server:

$ export YOURAPPLICATION_SETTINGS=/path/to/settings.cfg
$ python run-app.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader...

On Windows systems use the set builtin instead:

>set YOURAPPLICATION_SETTINGS=\path\to\settings.cfg

The configuration files themselves are actual Python files. Only values in uppercase are actually stored in the config object later on. So make sure to use uppercase letters for your config keys.

Here is an example configuration file:

DEBUG = False
SECRET_KEY = '?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83'

Make sure to load the configuration very early on so that extensions have the ability to access the configuration when starting up. There are other methods on the config object as well to load from individual files. For a complete reference, read the Config object’s documentation.

ベストな設定方法

The downside with the approach mentioned earlier is that it makes testing a little harder. There is no one 100% solution for this problem in general, but there are a couple of things you can do to improve that experience:

  1. create your application in a function and register modules on it. That way you can create multiple instances of your application with different configurations attached which makes unittesting a lot easier. You can use this to pass in configuration as needed.
  2. Do not write code that needs the configuration at import time. If you limit yourself to request-only accesses to the configuration you can reconfigure the object later on as needed.

開発環境 / 本番環境

Most applications need more than one configuration. There will at least be a separate configuration for a production server and one used during development. The easiest way to handle this is to use a default configuration that is always loaded and part of version control, and a separate configuration that overrides the values as necessary as mentioned in the example above:

app = Flask(__name__)
app.config.from_object('yourapplication.default_settings')
app.config.from_envvar('YOURAPPLICATION_SETTINGS')

Then you just have to add a separate config.py file and export YOURAPPLICATION_SETTINGS=/path/to/config.py and you are done. However there are alternative ways as well. For example you could use imports or subclassing.

What is very popular in the Django world is to make the import explicit in the config file by adding an from yourapplication.default_settings import * to the top of the file and then overriding the changes by hand. You could also inspect an environment variable like YOURAPPLICATION_MODE and set that to production, development etc and import different hardcoded files based on that.

An interesting pattern is also to use classes and inheritance for configuration:

class Config(object):
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite://:memory:'

class ProductionConfig(Config):
    DATABASE_URI = 'mysql://user@localhost/foo'

class DevelopmentConfig(Config):
    DEBUG = True

class TestinConfig(Config):
    TESTING = True

To enable such a config you just have to call into from_object():

app.config.from_object('configmodule.ProductionConfig')

There are many different ways and it’s up to you how you want to manage your configuration files. However here a list of good recommendations:

  • keep a default configuration in version control. Either populate the config with this default configuration or import it in your own configuration files before overriding values.
  • use an environment variable to switch between the configurations. This can be done from outside the Python interpreter and makes development and deployment much easier because you can quickly and easily switch between different configs without having to touch the code at all. If you are working often on different projects you can even create your own script for sourcing that activates a virtualenv and exports the development configuration for you.
  • Use a tool like fabric in production to push code and configurations sepearately to the production server(s). For some details about how to do that, head over to the deploy pattern.