<Flask> Flask-Script
Flask
でセットアップ、デバック等々が楽になるように、
Flask-Script
という拡張モジュール extentioin
があるのを知った。
試してみる。
もとのapp.py
スクリプトをコピーして、
script.py
という名前にして#追加
の行を追加。
from flask import Flask from flask.ext.script import Manager app = Flask(__name__) manager = Manager(app) # 追加 @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': manager.run() # 追加 # app.run(debug=True)
で、シェル Shell
上で動かす。
[3068:hage:works_flask]>python script.py usage: script.py [-?] {runserver,shell} ... positional arguments: {runserver,shell} runserver Runs the Flask development server i.e. app.run() shell Runs a Python shell inside Flask application context. optional arguments: -?, --help show this help message and exit
ふーん、、、
次でrunserver
のhelp
が起動。
[3069:hage:works_flask]>python script.py runserver --help usage: script.py runserver [-?] [-h HOST] [-p PORT] [--threaded] [--processes PROCESSES] [--passthrough-errors] [-d] [-D] [-r] [-R] Runs the Flask development server i.e. app.run() optional arguments: -?, --help show this help message and exit -h HOST, --host HOST -p PORT, --port PORT --threaded --processes PROCESSES --passthrough-errors -d, --debug enable the Werkzeug debugger (DO NOT use in production code) -D, --no-debug disable the Werkzeug debugger -r, --reload monitor Python files for changes (not 100{'choices': None, 'help': 'monitor Python files for changes (not 100% safe for production use)', 'nargs': 0, 'const': True, 'type': None, 'prog': 'script.py runserver', 'option_strings': ['-r', '--reload'], 'container': <argparse._ArgumentGroup object at 0x00000000038C3898>, 'dest': 'use_reloader', 'required': False, 'metavar': None, 'default': None}afe for production use) -R, --no-reload do not monitor Python files for changes
で、次のように、--port
オプションを取れば、
Flask
サーバがリスニングしているPORT
を変更できる。
[3070:hage:works_flask]>python script.py runserver --port 8000 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit) 127.0.0.1 - - [13/Apr/2016 13:52:55] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [13/Apr/2016 13:52:55] "GET /favicon.ico HTTP/1.1" 404 -
ふーん、、、
でも、ipython上から同じことすれば、いいんちゃう?
とも思ったりもした。
In [1]: import app In [2]: whos Variable Type Data/Info ------------------------------ app module <module 'app' from 'C:\\m<...>on\\works_flask\\app.py'> In [3]: app?? Type: module String form: <module 'app' from 'C:\\msys64\\home\\hage\\python\\works_flask\\app.py'> File: c:\msys64\home\hage\python\works_flask\app.py Source: from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run(debug=True) In [4]: from app import app In [5]: whos Variable Type Data/Info ----------------------------- app Flask <Flask 'app'> In [6]: app.run(port=7000) * Running on http://127.0.0.1:7000/ (Press CTRL+C to quit)
マニュアル
Flask-Script — Flask-Script 0.4.0 documentation
参考