Flask で大きなアプリケーション Blueprint ver.

Flask なんでそこまで大規模ではないですが、
1ファイルでは厳しくなってたので分割しました。
Flask 0.7.2 を使用で、


より大きなアプリケーション ― Flask v0.5.1 documentation
http://a2c.bitbucket.org/flask/patterns/packages.html


に従って、Module で定義すると、起動時に次のような警告。
(起動はするので動きはする

main.py:7: DeprecationWarning: Modules are deprecated. Upgrade to using blueprints. Have a look into the documentation for more information. If this module was registered by a Flask-Extension upgrade the extension or contact the author of that extension instead. (Registered )
app.register_module(root.app, url_prefix="/")


抜粋+意訳
「Modules are deprecated.」Module は非推奨です。
「Upgrade to using blueprints.」blueprint を使いましょう。


というわけで、どうやら v0.7 から Module は非推奨になった模様…。

register_module(module, **options)
(中略)
 Changed in version 0.7: The module system was deprecated in favor for the blueprint system.

http://flask.pocoo.org/docs/api/?highlight=module#flask.Flask.register_module


じゃあどうやって Blueprint に対応すんべ?と思ったら、
日本語情報がまだなかったのでメモ書きしておきます。


フォルダ構成は以下の通り。

/app/main.py
/app/views/__int__,py
/app/views/root.py
/app/views/sub.py
/app/templates/index.html
/app/templates/index_sub.html


まず、修正前

/app/main.py

# -*- coding: utf-8 -*-
from flask import Flask

app = Flask(__name__)

from views import root
app.register_module(root.app, url_prefix="/")

from views import sub
app.register_module(sub.app, url_prefix="/sub")


if __name__ == '__main__':
    app.run(debug=True)


/app/views/root.py

# -*- coding: utf-8 -*-
from flask import Module, render_template
app = Module(__name__)


@app.route("/")
def index():
    return render_template("index.html")


/app/views/sub.py

# -*- coding: utf-8 -*-
from flask import Module, render_template
app = Module(__name__)


@app.route("/")
def index():
    return render_template("index_sub.html")

html は割愛。


これを blueprint 対応に修正します。
修正箇所は主に

 - app.register_module(root.app, url_prefix="/")
 + app.register_blueprint(root.app, url_prefix="/")

とか

 - app = Module(__name__)
 + app = Blueprint(__name__, "root")

それくらい。


/app/main.py

# -*- coding: utf-8 -*-
from flask import Flask

app = Flask(__name__)

from views import root
app.register_blueprint(root.app, url_prefix="/")

from views import sub
app.register_blueprint(sub.app, url_prefix="/sub")


if __name__ == '__main__':
    app.run(debug=True)


/app/views/root.py

# -*- coding: utf-8 -*-
from flask import Blueprint, render_template
app = Blueprint(__name__, "root")


@app.route("/")
def index():
    return render_template("index.html")


/app/views/sub.py

# -*- coding: utf-8 -*-
from flask import Blueprint, render_template
app = Blueprint(__name__, "sub")


@app.route("/")
def index():
    return render_template("index_sub.html")

Blueprint になってテンプレートフォルダの明示とか出来るようになったりしているので、使いこなせば便利そう。
ただ、まだ v0.7 系が出て2ヶ月程しか経ってないようなので日本語ドキュメント少なし。
http://flask.pocoo.org/docs/changelog/#version-0-7


# こういうドキュメントの和訳もまたやりたいなぁ…。


以下、必要になりそうなのでメモ貼り。

Flask の Blueprints で app オブジェクトが欲しくなったら - Heavens hell
http://d.hatena.ne.jp/heavenshell/20110824/1314190929