Flask是一個(gè)Python編寫的Web 微框架剿吻,讓我們可以使用Python語言快速實(shí)現(xiàn)一個(gè)網(wǎng)站或Web服務(wù)。本文參考自Flask官方文檔涤躲,大部分代碼引用自官方文檔县遣。
安裝Flask
首先我們來安裝Flask。最簡(jiǎn)單的辦法就是使用pip耕拷。
pip install flask
然后打開一個(gè)Python文件讼昆,輸入下面的內(nèi)容并運(yùn)行該文件。然后訪問localhost:5000
骚烧,我們應(yīng)當(dāng)可以看到瀏覽器上輸出了Hello Flask!
浸赫。
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello Flask!'
if __name__ == '__main__':
app.run()
快速開始
調(diào)試模式
我們修改代碼中的輸出,然后查看瀏覽器上是否有變化赃绊。如果你照做的話既峡,可以看到什么變化都沒有。其實(shí)Flask內(nèi)置了調(diào)試模式碧查,可以自動(dòng)重載代碼并顯示調(diào)試信息运敢。這需要我們開啟調(diào)試模式,方法很簡(jiǎn)單忠售,設(shè)置FLASK_DEBUG
環(huán)境變量传惠,并將值設(shè)置為1
。
然后再次運(yùn)行程序档痪,會(huì)看到有這樣的輸出涉枫。這時(shí)候如果再次修改代碼,會(huì)發(fā)現(xiàn)這次Flask會(huì)自動(dòng)重啟腐螟。
* Restarting with stat
* Debugger is active!
* Debugger PIN: 157-063-180
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
路由
在上面的例子里可以看到路由的使用愿汰。如果了解Spring Web MVC的話,應(yīng)該對(duì)路由很熟悉乐纸。路由通過使用Flask的app.route
裝飾器來設(shè)置衬廷,這類似Java的注解。
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World'
路徑變量
如果希望獲取/article/1
這樣的路徑參數(shù)汽绢,就需要使用路徑變量吗跋。路徑變量的語法是/path/<converter:varname>
。在路徑變量前還可以使用可選的轉(zhuǎn)換器宁昭,有以下幾種轉(zhuǎn)換器跌宛。
轉(zhuǎn)換器 | 作用 |
---|---|
string | 默認(rèn)選項(xiàng),接受除了斜杠之外的字符串 |
int | 接受整數(shù) |
float | 接受浮點(diǎn)數(shù) |
path | 和string類似积仗,不過可以接受帶斜杠的字符串 |
any | 匹配任何一種轉(zhuǎn)換器 |
uuid | 接受UUID字符串 |
下面是Flask官方的例子疆拘。
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
構(gòu)造URL
在Web程序中常常需要獲取某個(gè)頁面的URL,在Flask中需要使用url_for('方法名')
來構(gòu)造對(duì)應(yīng)方法的URL寂曹。下面是Flask官方的例子哎迄。
>>> from flask import Flask, url_for
>>> app = Flask(__name__)
>>> @app.route('/')
... def index(): pass
...
>>> @app.route('/login')
... def login(): pass
...
>>> @app.route('/user/<username>')
... def profile(username): pass
...
>>> with app.test_request_context():
... print url_for('index')
... print url_for('login')
... print url_for('login', next='/')
... print url_for('profile', username='John Doe')
...
/
/login
/login?next=/
/user/John%20Doe
HTTP方法
如果需要處理具體的HTTP方法回右,在Flask中也很容易,使用route
裝飾器的methods
參數(shù)設(shè)置即可漱挚。
from flask import request
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
do_the_login()
else:
show_the_login_form()
靜態(tài)文件
Web程序中常常需要處理靜態(tài)文件翔烁,在Flask中需要使用url_for
函數(shù)并指定static
端點(diǎn)名和文件名。在下面的例子中旨涝,實(shí)際的文件應(yīng)放在static/
文件夾下蹬屹。
url_for('static', filename='style.css')
模板生成
Flask默認(rèn)使用Jinja2作為模板,F(xiàn)lask會(huì)自動(dòng)配置Jinja 模板白华,所以我們不需要其他配置了哩治。默認(rèn)情況下,模板文件需要放在templates
文件夾下衬鱼。
使用 Jinja 模板,只需要使用render_template
函數(shù)并傳入模板文件名和參數(shù)名即可憔杨。
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
相應(yīng)的模板文件如下鸟赫。
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello, World!</h1>
{% endif %}
日志輸出
Flask 為我們預(yù)配置了一個(gè) Logger,我們可以直接在程序中使用消别。這個(gè)Logger是一個(gè)標(biāo)準(zhǔn)的Python Logger抛蚤,所以我們可以向標(biāo)準(zhǔn)Logger那樣配置它,詳情可以參考官方文檔或者我的文章Python 日志輸出寻狂。
app.logger.debug('A value for debugging')
app.logger.warning('A warning occurred (%d apples)', 42)
app.logger.error('An error occurred')
處理請(qǐng)求
在 Flask 中獲取請(qǐng)求參數(shù)需要使用request
等幾個(gè)全局對(duì)象岁经,但是這幾個(gè)全局對(duì)象比較特殊,它們是 Context Locals 蛇券,其實(shí)就是 Web 上下文中局部變量的代理缀壤。雖然我們?cè)诔绦蛑惺褂玫氖侨肿兞浚菍?duì)于每個(gè)請(qǐng)求作用域纠亚,它們都是互不相同的變量塘慕。理解了這一點(diǎn),后面就非常簡(jiǎn)單了蒂胞。
Request 對(duì)象
Request 對(duì)象是一個(gè)全局對(duì)象图呢,利用它的屬性和方法,我們可以方便的獲取從頁面?zhèn)鬟f過來的參數(shù)骗随。
method
屬性會(huì)返回HTTP方法的類似蛤织,例如post
和get
。form
屬性是一個(gè)字典鸿染,如果數(shù)據(jù)是POST類型的表單指蚜,就可以從form
屬性中獲取。下面是 Flask 官方的例子牡昆,演示了 Request 對(duì)象的method
和form
屬性姚炕。
from flask import request
@app.route('/login', methods=['POST', 'GET'])
def login():
error = None
if request.method == 'POST':
if valid_login(request.form['username'],
request.form['password']):
return log_the_user_in(request.form['username'])
else:
error = 'Invalid username/password'
# the code below is executed if the request method
# was GET or the credentials were invalid
return render_template('login.html', error=error)
如果數(shù)據(jù)是由GET方法傳送過來的摊欠,可以使用args
屬性獲取,這個(gè)屬性也是一個(gè)字典柱宦。
searchword = request.args.get('key', '')
文件上傳
利用Flask也可以方便的獲取表單中上傳的文件些椒,只需要利用 request 的files
屬性即可,這也是一個(gè)字典掸刊,包含了被上傳的文件免糕。如果想獲取上傳的文件名,可以使用filename
屬性忧侧,不過需要注意這個(gè)屬性可以被客戶端更改石窑,所以并不可靠。更好的辦法是利用werkzeug
提供的secure_filename
方法來獲取安全的文件名蚓炬。
from flask import request
from werkzeug.utils import secure_filename
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['the_file']
f.save('/var/www/uploads/' + secure_filename(f.filename))
Cookies
Flask也可以方便的處理Cookie松逊。使用方法很簡(jiǎn)單,直接看官方的例子就行了肯夏。下面的例子是如何獲取cookie经宏。
from flask import request
@app.route('/')
def index():
username = request.cookies.get('username')
# 使用 cookies.get(key) 代替 cookies[key] 避免
# 得到 KeyError 如果cookie不存在
如果需要發(fā)送cookie給客戶端,參考下面的例子驯击。
from flask import make_response
@app.route('/')
def index():
resp = make_response(render_template(...))
resp.set_cookie('username', 'the username')
return resp
重定向和錯(cuò)誤
redirect
和abort
函數(shù)用于重定向和返回錯(cuò)誤頁面烁兰。
from flask import abort, redirect, url_for
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/login')
def login():
abort(401)
this_is_never_executed()
默認(rèn)的錯(cuò)誤頁面是一個(gè)空頁面,如果需要自定義錯(cuò)誤頁面徊都,可以使用errorhandler
裝飾器沪斟。
from flask import render_template
@app.errorhandler(404)
def page_not_found(error):
return render_template('page_not_found.html'), 404
響應(yīng)處理
默認(rèn)情況下,F(xiàn)lask會(huì)根據(jù)函數(shù)的返回值自動(dòng)決定如何處理響應(yīng):如果返回值是響應(yīng)對(duì)象暇矫,則直接傳遞給客戶端主之;如果返回值是字符串,那么就會(huì)將字符串轉(zhuǎn)換為合適的響應(yīng)對(duì)象李根。我們也可以自己決定如何設(shè)置響應(yīng)對(duì)象杀餐,方法也很簡(jiǎn)單,使用make_response
函數(shù)即可朱巨。
@app.errorhandler(404)
def not_found(error):
resp = make_response(render_template('error.html'), 404)
resp.headers['X-Something'] = 'A value'
return resp
Sessions
我們可以使用全局對(duì)象session
來管理用戶會(huì)話史翘。Sesison 是建立在 Cookie 技術(shù)上的,不過在 Flask 中冀续,我們還可以為 Session 指定密鑰琼讽,這樣存儲(chǔ)在 Cookie 中的信息就會(huì)被加密,從而更加安全洪唐。直接看 Flask 官方的例子吧钻蹬。
from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
@app.route('/')
def index():
if 'username' in session:
return 'Logged in as %s' % escape(session['username'])
return 'You are not logged in'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=submit value=Login>
</form>
'''
@app.route('/logout')
def logout():
# remove the username from the session if it's there
session.pop('username', None)
return redirect(url_for('index'))
# set the secret key. keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
模板簡(jiǎn)介
這里簡(jiǎn)單的介紹一下Jinja 模板的使用方法,詳細(xì)資料直接看原文檔吧凭需。
模板標(biāo)簽
其實(shí)Jinja 模板和其他語言和框架的模板類似问欠,反正都是通過某種語法將HTML文件中的特定元素替換為實(shí)際的值肝匆。如果使用過JSP、Thymeleaf 等模板顺献,應(yīng)該可以非常容易的學(xué)會(huì)使用 Jinja模板旗国。
其實(shí)從上面的例子中我們應(yīng)該可以看到Jinja 模板的基本語法了。代碼塊需要包含在{% %}
塊中注整,例如下面的代碼能曾。
{% extends 'layout.html' %}
{% block title %}主頁{% endblock %}
{% block body %}
<div class="jumbotron">
<h1>主頁</h1>
</div>
{% endblock %}
雙大括號(hào)中的內(nèi)容不會(huì)被轉(zhuǎn)義,所有內(nèi)容都會(huì)原樣輸出肿轨,它常常和其他輔助函數(shù)一起使用寿冕。下面是一個(gè)例子。
<a class="navbar-brand" href={{ url_for('index') }}>Flask小例子</a>
繼承
模板可以繼承其他模板椒袍,我們可以將布局設(shè)置為父模板驼唱,讓其他模板繼承,這樣可以非常方便的控制整個(gè)程序的外觀驹暑。
例如這里有一個(gè)layout.html
模板曙蒸,它是整個(gè)程序的布局文件。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static',filename='css/bootstrap.css') }}"/>
<link rel="stylesheet" href="{{ url_for('static',filename='css/bootstrap-theme.css') }}"/>
</head>
<body>
<div class="container body-content">
{% block body %}{% endblock %}
</div>
<div class="container footer">
<hr>
<p>這是頁腳</p>
</div>
<script src="{{ url_for('static',filename='js/jquery.js') }}"></script>
<script src="{{ url_for('static',filename='js/bootstrap.js') }}"></script>
</body>
</html>
其他模板可以這么寫岗钩。對(duì)比一下面向?qū)ο缶幊痰睦^承概念,我們可以很容易的理解肖油。
{% extends 'layout.html' %}
{% block title %}主頁{% endblock %}
{% block body %}
<div class="jumbotron">
<h1>主頁</h1>
<p>本項(xiàng)目演示了Flask的簡(jiǎn)單使用方法兼吓,點(diǎn)擊導(dǎo)航欄上的菜單條查看具體功能。</p>
</div>
{% endblock %}
控制流
條件判斷可以這么寫森枪,類似于JSP標(biāo)簽中的Java 代碼视搏,{% %}
中也可以寫Python代碼。下面是Flask官方文檔的例子县袱。
<div class=metanav>
{% if not session.logged_in %}
<a href="{{ url_for('login') }}">log in</a>
{% else %}
<a href="{{ url_for('logout') }}">log out</a>
{% endif %}
</div>
循環(huán)的話可以這么寫浑娜,和在Python中遍歷差不多。
<tbody>
{% for key,value in data.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
<tr>
<td>文件</td>
<td></td>
</tr>
</tbody>
需要注意不是所有的Python代碼都可以寫在模板里式散,如果希望從模板中引用其他文件的函數(shù)筋遭,需要顯式將函數(shù)注冊(cè)到模板中”┲簦可以參考這個(gè)爆棧提問漓滔。
寫在最后
這篇文章主要參考了Flask的官方文檔,但是只介紹了 Flask的最基本的一部分乖篷。了解了這部分响驴,我們可以用Python 搭一個(gè)小服務(wù)器做點(diǎn)事情。如果希望詳細(xì)了解 Flask的使用用法撕蔼,請(qǐng)關(guān)注更詳細(xì)的資料豁鲤。本文就是起一個(gè)拋磚引玉的效果秽誊。
順便說,通過Flask 我也了解了Python 語言的執(zhí)行速度琳骡。我們都知道編譯器編譯出來的代碼執(zhí)行起來要比解釋器解釋代碼要快大約幾十倍到幾千倍不等锅论。以前學(xué)Java的時(shí)候,感覺Java 慢日熬,主要原因就是等待編譯時(shí)間比較長(zhǎng)棍厌。相對(duì)來說用Python寫腳本就很塊了,因?yàn)闆]有編譯過程竖席。
但是從Flask的運(yùn)行速度來看耘纱,我切身感受到了Python 執(zhí)行確實(shí)不快。舉個(gè)例子毕荐,在Spring中寫一個(gè)控制器束析,接受HTTP參數(shù),并顯示到頁面上憎亚,如果程序編譯完之后员寇,這個(gè)顯示過程基本是瞬時(shí)的。但是同樣的需求在Flask中第美,我居然可以感覺到明顯的延遲(大概幾百毫秒的等待時(shí)間)蝶锋。所以,如果你想寫一個(gè)比較快的Web程序什往,還是用Java或者JVM語言吧扳缕,雖然看著土,性能確實(shí)杠杠的 别威。
最后躯舔,我寫了一個(gè)小練習(xí),試了試Flask 的基本功能省古,如果有興趣可以上我的Github查看代碼粥庄。