環境需求:python2.7以上, python套件pip & virtualenv安裝
0. 建立virtualenv環境, 使用virturlenv目的就是為了建立一個乾淨的開發環境, 可以去掌握你撰寫的python程式跟其他package的相依性, 利於你的程式部署到其他機器上
$ pip install virtualenv Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Python/2.7/site-packages Cleaning up... $ virtualenv .env New python executable in .env/bin/python Installing Setuptools..............................................................................................................................................................................................................................done. Installing Pip.....................................................................................................................................................................................................................................................................................................................................done. $ source .env/bin/activate #進入virtualenv環境 (.env) $1. 在virtual environment 下安裝 Flask
$ virtualenv .env (.env) $ pip install flask2. 產生app instance
from flask import Flask app = Flask(__name__)3. 建立Route
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello World !!!' @app.route('/user/<name>/') def user(name): return 'hello %s' % name if __name__ == '__main__': app.run(debug=True);4. run script by python interpreter
$(.env) python hello.py *Running on http://127.0.0.1:5000/ *Restarting with reloader接下來可以打開瀏覽器輸入http://127.0.0.1:5000/看看
還有http://127.0.0.1:5000/user/test
http://127.0.0.1:5000/user/world
0 意見:
張貼留言