一、環境配置
當前我的開發環境是Miniconda3+PyCharm。開發環境其實無所謂,自己使用Python3+Nodepad都可以。安裝Flask庫:
pip install Flask
二、第一個應用程序
將以下內容保存為helloworld.py:
# 導入Flask類 from flask import Flask # 實例化,可視為固定格式 app = Flask(__name__) # route()方法用於設定路由;類似spring路由配置 @app.route('/helloworld') def hello_world(): return 'Hello, World!' if __name__ == '__main__': # app.run(host, port, debug, options) # 默認值:host="127.0.0.1", port=5000, debug=False app.run(host="0.0.0.0", port=5000)
直接運行該文件,然后訪問:http://127.0.0.1:5000/helloworld。結果如下圖:
三、get和post實現
3.1 創建用到的模板文件
Flask默認到templates目錄下查找模板文件,在上邊helloworld.py同級目錄下創建templates文件夾。
在templates文件夾下創建get.html,寫入以下內容:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>get請求示例</title> </head> <body> <form action="/deal_request" method="get"> <input type="text" name="q" /> <input type="submit" value="搜索" /> </form> </body> </html>
再在templates文件夾下創建post.html,寫入以下內容:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>post請求示例</title> </head> <body> <form action="/deal_request" method="post"> <input type="text" name="q" /> <input type="submit" value="搜索" /> </form> </body> </html>
最后在templates文件夾下創建result.html,寫入以下內容:
<!-- Flask 使用Jinja2模板引擎,Jinja2模板引擎源於Django板模所以很多語法和Django是類似的 --> <h1>{{ result }}</h1>
3.2 編寫相關的處理方法
在helloworld.py中添加get_html()、post_html()和deal_request()三個方法,更多說明見注釋。當前helloworld.py內容如下:
# 導入Flask類 from flask import Flask from flask import render_template from flask import request # 實例化,可視為固定格式 app = Flask(__name__) # route()方法用於設定路由;類似spring路由配置 #等價於在方法后寫:app.add_url_rule('/', 'helloworld', hello_world) @app.route('/helloworld') def hello_world(): return 'Hello, World!' # 配置路由,當請求get.html時交由get_html()處理 @app.route('/get.html') def get_html(): # 使用render_template()方法重定向到templates文件夾下查找get.html文件 return render_template('get.html') # 配置路由,當請求post.html時交由post_html()處理 @app.route('/post.html') def post_html(): # 使用render_template()方法重定向到templates文件夾下查找post.html文件 return render_template('post.html') # 配置路由,當請求deal_request時交由deal_request()處理 # 默認處理get請求,我們通過methods參數指明也處理post請求 # 當然還可以直接指定methods = ['POST']只處理post請求, 這樣下面就不需要if了 @app.route('/deal_request', methods = ['GET', 'POST']) def deal_request(): if request.method == "GET": # get通過request.args.get("param_name","")形式獲取參數值 get_q = request.args.get("q","") return render_template("result.html", result=get_q) elif request.method == "POST": # post通過request.form["param_name"]形式獲取參數值 post_q = request.form["q"] return render_template("result.html", result=post_q) if __name__ == '__main__': # app.run(host, port, debug, options) # 默認值:host=127.0.0.1, port=5000, debug=false app.run()
3.3 查看運行效果
重新運行helloworld.py。
當前目錄結構如下(.idea目錄不用管):
get.html如下:
get查詢結果如下:
post.html如下:
post查詢結果如下:
四、restful
4.1 安裝flask-restful
restful我這里通過Flask-RESTful實現。
pip install flask-restful
4.2 實現rest代碼
修改helloworld.py,添加三處,具體見注釋
# 導入Flask類 from flask import Flask from flask import render_template from flask import request # 添加位置一。從Flask-RESTful中導入以下三項 from flask_restful import Api from flask_restful import Resource from flask_restful import reqparse # 實例化,可視為固定格式 app = Flask(__name__) # 添加位置二。 # 實例化一個api用於配置rest路由 # 實例化一個參數解析類,用於rest獲取get和post提交的參數 api = Api(app) parser = reqparse.RequestParser() # 注冊q參數parser才能解析get和post中的q參數。這種注冊才能解析的要求是否有點孤兒 parser.add_argument('q', type=str, help='Rate to charge for this resource') # route()方法用於設定路由;類似spring路由配置 #等價於在方法后寫:app.add_url_rule('/', 'helloworld', hello_world) @app.route('/helloworld') def hello_world(): return 'Hello, World!' # 配置路由,當請求get.html時交由get_html()處理 @app.route('/get.html') def get_html(): # 使用render_template()方法重定向到templates文件夾下查找get.html文件 return render_template('get.html') # 配置路由,當請求post.html時交由post_html()處理 @app.route('/post.html') def post_html(): # 使用render_template()方法重定向到templates文件夾下查找post.html文件 return render_template('post.html') # 配置路由,當請求deal_request時交由deal_request()處理 # 默認處理get請求,我們通過methods參數指明也處理post請求 # 當然還可以直接指定methods = ['POST']只處理post請求, 這樣下面就不需要if了 @app.route('/deal_request', methods = ['GET', 'POST']) def deal_request(): if request.method == "GET": # get通過request.args.get("param_name","")形式獲取參數值 get_q = request.args.get("q","") return render_template("result.html", result=get_q) elif request.method == "POST": # post通過request.form["param_name"]形式獲取參數值 post_q = request.form["q"] return render_template("result.html", result=post_q) # 添加位置三。 # 添加rest類(是類而不是和Flask一樣直接是方法) class Rest(Resource): # get提交時的處理方法 def get(self): result = {} args = parser.parse_args() result["method"] = "get" result["q"] = args["q"] return result # post提交時的處理方法 def post(self): result = {} # 此種方法即可解析通過普通post提交也可解析json格式提交 args = parser.parse_args() result["method"] = "post" result["q"] = args["q"] return result # 配置路由 api.add_resource(Rest, '/rest') if __name__ == '__main__': # app.run(host, port, debug, options) # 默認值:host=127.0.0.1, port=5000, debug=false app.run()
4.3 查看運行結果
即然是restful我們也就不寫html了,直接用curl模擬提交。重新運行helloworld.py,請求如下圖(之前非rest的get和post當然也還能正常訪問):
五、Flask與Django比較
我們經常會聽說這樣的一個觀點:Django是Python最流行的Web框架但配置比較復雜,Flask是一個輕量級的框架配置比較簡單如果項目比較小推薦使用Flask。
5.1 Django配置復雜
如果對Django不是很了解,可以參看“Python3+PyCharm+Django+Django REST framework開發教程”和“Python3+Django get/post請求實現教程 ”。
僅從文章長度看就比這篇長很多,所以Django比Flask復雜(得多)是肯定的。更具體比較如下:
比較項 | Django | Flask | 復雜度比較 | 說明 |
項目創建 | Django需要用命令創建項目 | Flask直接編寫文件就可運行 | Django復雜 | Django需要用命令創建項目是因為要創建出整個項目框架 |
路由 | Django使用專門的urls.py文件 | Flask直接使用@app.route() | Django笨重 | Django類似Strut2的配置Flask類似Spring的配置,Flask感覺更好 |
get和post | request.GET['name']和request.POST["name"] | request.args.get("name","")和request.form["q"] | 差不多 | Flask格式上不統一 |
restful | 使用django-resful框架 | 使用flask-restful框架 | 差不多 | Flask使用方法flask-rest使用類格式不統一;flask-restful需要注冊參數有點孤兒 |
數據庫操作 | django集成了對數據庫的操作 | Flask沒集成對數據庫的操作要另行直連或使用sqlalchemy | 差不多 | django復雜很大程度來源於對數據庫的集成。 |
5.2 Flask適合使用場景
Django復雜來源於其“可能用到的功能都先集成”,Flask的輕量來源其“暫時不用的功能都先不做處理”。隨着項目規模的擴大最終Django有的東西Flask也都需要有,而且由於沒做統一規划Flask將處於劣勢位置。
個人認為Flask適用以下兩個場景:
一是開發者沒有Spring等企業級框架開發經驗,不然Django很多項為什么要配置為什么這樣配置不那樣配置比較難理解。
二是項目大概是十來個頁面、就只有兩三張數據表甚至不用數據庫的規模,在這種程度下減少的配置工作還算能抵得過增加的開發工作量。
如果之前用過Spring之類的框架寫過代碼的話個人還是推薦不管項目大小都直接用Django,主要是Flask不統一的格式讓人很難受。
參考: