Flask基本
Installation
環境
- Ubuntu14.04
pip install
$ pip install Flask $ python hello.py * Running on http://localhost:5000/
Getting Started
Super Basics
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
Restful API
Extensionは必要ないと言っている
<type:variable>
するだけ
methodは,methods=[]に書く
... @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT']) def update_task(task_id): ...
Large app structure recommandation
~/LargeApp |-- run.py |-- config.py |-- shell.py # No exsistent on this reference originally, but helpful to init db. |__ /env # Virtual Environment |__ /app # Our Application Module |-- __init__.py |-- /module_one |-- __init__.py |-- controllers.py |-- models.py |-- services.py |__ /templates |__ /module_one |-- hello.html |__ /static |__ .. |__ . |__ .. |__ .
基本設計
controllers.py:
1. blueprintでmoduleを生成
2. dbインスタンスはapp/__init__.pyで生成したものをimportする
3. url routing
models.py
- orm
services.py
- logic
app dirの下にある__init__.py: appの初期値化
これを参考.
- __init__.py
- 1. app, db instanceの生成
- 2. controllersの読み込み
- 3. modulesをblueprintに登録登録
Blueprint
- MVT: model view template
- MVC的には,MVT=MVC (Model=Model, View=Controller, Template=View)になる
- viewがcontrollerの役割をする
- appのグループ化に使う
基本的な構築方法
- module
mod_auth = Blueprint('auth', __name__, url_prefix='/auth')
- register
from app.mod_auth.controllers import mod_auth as auth_module * Register blueprint(s) app.register_blueprint(auth_module)
modelの生成とregisterをどこでやるかは設計者による.
極論だとどういう構成でもいい.
参考URLs
Tutorials
Restful api
Large appの設計
- https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications
- https://github.com/mitsuhiko/flask/wiki/Large-app-how-to
- http://flask.pocoo.org/snippets/category/application-structure/
- http://qiita.com/Alice1017/items/a6b6500e60f2a0334e44
- http://flask.pocoo.org/docs/0.10/blueprints/