Flask是一個Python編寫的Web 微框架,讓咱們可使用Python語言快速實現一個網站或Web服務。本文參考自Flask官方文檔,大部分代碼引用自官方文檔。css
安裝flaskhtml
首先咱們來安裝Flask。最簡單的辦法就是使用pip。python
pip install flask
而後打開一個Python文件,輸入下面的內容並運行該文件。而後訪問localhost:5000
,咱們應當能夠看到瀏覽器上輸出了hello world。jquery
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'hello world' if __name__ == '__main__': app.run(host='127.0.0.1',port=5000)
咱們修改代碼中的輸出,而後查看瀏覽器上是否有變化。若是你照作的話,能夠看到什麼變化都沒有。其實Flask內置了調試模式,能夠自動重載代碼並顯示調試信息。這須要咱們開啓調試模式,方法很簡單,設置FLASK_DEBUG
環境變量,並將值設置爲1
。或者設置app.debug=True編程
from flask import Flask app = Flask(__name__) app.debug=True @app.route('/') def hello_world(): return 'Hello World!' @app.route('/login') def login(): return 'Login' if __name__ == '__main__': app.run()
而後再次運行程序,會看到有這樣的輸出。這時候若是再次修改代碼,會發現此次Flask會自動重啓。flask
* 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的話,應該對路由很熟悉。路由經過使用Flask的app.route
裝飾器來設置,這相似Java的註解。bootstrap
@app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello, World'
若是但願獲取/article/1
這樣的路徑參數,就須要使用路徑變量。路徑變量的語法是/path/<converter:varname>
。在路徑變量前還可使用可選的轉換器,有如下幾種轉換器。瀏覽器
轉換器 | 做用 |
---|---|
string | 默認選項,接受除了斜槓以外的字符串 |
int | 接受整數 |
float | 接受浮點數 |
path | 和string相似,不過能夠接受帶斜槓的字符串 |
any | 匹配任何一種轉換器 |
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
在Web程序中經常須要獲取某個頁面的URL,在Flask中須要使用url_for('方法名')
來構造對應方法的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方法,在Flask中也很容易,使用route
裝飾器的methods
參數設置便可。
from flask import request @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form()
Web程序中經常須要處理靜態文件,在Flask中須要使用url_for
函數並指定static
端點名和文件名。在下面的例子中,實際的文件應放在static/
文件夾下。
url_for('static', filename='style.css')
Flask默認使用Jinja2做爲模板,Flask會自動配置Jinja 模板,因此咱們不須要其餘配置了。默認狀況下,模板文件須要放在templates
文件夾下。
使用 Jinja 模板,只須要使用render_template
函數並傳入模板文件名和參數名便可。
from flask import render_template @app.route('/hello/') @app.route('/hello/<name>') def hello(name=None): return render_template('hello.html', name=name)
相應的模板文件以下。
<!doctype html> <title>Hello from Flask</title> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello, World!</h1> {% endif %}
Flask 爲咱們預配置了一個 Logger,咱們能夠直接在程序中使用。這個Logger是一個標準的Python Logger,因此咱們能夠向標準Logger那樣配置它,詳情能夠參考官方文檔或者個人文章Python 日誌輸出。
app.logger.debug('A value for debugging') app.logger.warning('A warning occurred (%d apples)', 42) app.logger.error('An error occurred')
在 Flask 中獲取請求參數須要使用request
等幾個全局對象,可是這幾個全局對象比較特殊,它們是 Context Locals ,其實就是 Web 上下文中局部變量的代理。雖然咱們在程序中使用的是全局變量,可是對於每一個請求做用域,它們都是互不相同的變量。理解了這一點,後面就很是簡單了。
Request 對象是一個全局對象,利用它的屬性和方法,咱們能夠方便的獲取從頁面傳遞過來的參數。
method
屬性會返回HTTP方法的相似,例如post
和get
。form
屬性是一個字典,若是數據是POST類型的表單,就能夠從form
屬性中獲取。下面是 Flask 官方的例子,演示了 Request 對象的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)
若是數據是由GET方法傳送過來的,可使用args
屬性獲取,這個屬性也是一個字典。
searchword = request.args.get('key', '')
利用Flask也能夠方便的獲取表單中上傳的文件,只須要利用 request 的files
屬性便可,這也是一個字典,包含了被上傳的文件。若是想獲取上傳的文件名,可使用filename
屬性,不過須要注意這個屬性能夠被客戶端更改,因此並不可靠。更好的辦法是利用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))
Flask也能夠方便的處理Cookie。使用方法很簡單,直接看官方的例子就好了。下面的例子是如何獲取cookie。
from flask import request @app.route('/') def index(): username = request.cookies.get('username') # 使用 cookies.get(key) 代替 cookies[key] 避免 # 獲得 KeyError 若是cookie不存在
若是須要發送cookie給客戶端,參考下面的例子。
from flask import make_response @app.route('/') def index(): resp = make_response(render_template(...)) resp.set_cookie('username', 'the username') return resp
redirect
和abort
函數用於重定向和返回錯誤頁面。
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()
默認的錯誤頁面是一個空頁面,若是須要自定義錯誤頁面,可使用errorhandler
裝飾器。
from flask import render_template @app.errorhandler(404) def page_not_found(error): return render_template('page_not_found.html'), 404
默認狀況下,Flask會根據函數的返回值自動決定如何處理響應:若是返回值是響應對象,則直接傳遞給客戶端;若是返回值是字符串,那麼就會將字符串轉換爲合適的響應對象。咱們也能夠本身決定如何設置響應對象,方法也很簡單,使用make_response
函數便可。
@app.errorhandler(404) def not_found(error): resp = make_response(render_template('error.html'), 404) resp.headers['X-Something'] = 'A value' return resp
咱們可使用全局對象session
來管理用戶會話。Sesison 是創建在 Cookie 技術上的,不過在 Flask 中,咱們還能夠爲 Session 指定密鑰,這樣存儲在 Cookie 中的信息就會被加密,從而更加安全。直接看 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'
這裏簡單的介紹一下Jinja 模板的使用方法,詳細資料直接看原文檔吧。
其實Jinja 模板和其餘語言和框架的模板相似,反正都是經過某種語法將HTML文件中的特定元素替換爲實際的值。若是使用過JSP、Thymeleaf 等模板,應該能夠很是容易的學會使用 Jinja模板。
其實從上面的例子中咱們應該能夠看到Jinja 模板的基本語法了。代碼塊須要包含在{% %}
塊中,例以下面的代碼。
{% extends 'layout.html' %} {% block title %}主頁{% endblock %} {% block body %} <div class="jumbotron"> <h1>主頁</h1> </div> {% endblock %}
雙大括號中的內容不會被轉義,全部內容都會原樣輸出,它經常和其餘輔助函數一塊兒使用。下面是一個例子。
<a class="navbar-brand" href={{ url_for('index') }}>Flask小例子</a>
模板能夠繼承其餘模板,咱們能夠將佈局設置爲父模板,讓其餘模板繼承,這樣能夠很是方便的控制整個程序的外觀。
例如這裏有一個layout.html
模板,它是整個程序的佈局文件。
<!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>
其餘模板能夠這麼寫。對比一下面向對象編程的繼承概念,咱們能夠很容易的理解。
{% extends 'layout.html' %} {% block title %}主頁{% endblock %} {% block body %} <div class="jumbotron"> <h1>主頁</h1> <p>本項目演示了Flask的簡單使用方法,點擊導航欄上的菜單條查看具體功能。</p> </div> {% endblock %}
條件判斷能夠這麼寫,相似於JSP標籤中的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>
循環的話能夠這麼寫,和在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代碼均可以寫在模板裏,若是但願從模板中引用其餘文件的函數,須要顯式將函數註冊到模板中。能夠參考這個爆棧提問。
這篇文章主要參考了Flask的官方文檔,可是隻介紹了 Flask的最基本的一部分。瞭解了這部分,咱們能夠用Python 搭一個小服務器作點事情。若是但願詳細瞭解 Flask的使用用法,請關注更詳細的資料。本文就是起一個拋磚引玉的效果。
順便說,經過Flask 我也瞭解了Python 語言的執行速度。咱們都知道編譯器編譯出來的代碼執行起來要比解釋器解釋代碼要快大約幾十倍到幾千倍不等。之前學Java的時候,感受Java 慢,主要緣由就是等待編譯時間比較長。相對來講用Python寫腳本就很塊了,由於沒有編譯過程。
可是從Flask的運行速度來看,我切身感覺到了Python 執行確實不快。舉個例子,在Spring中寫一個控制器,接受HTTP參數,並顯示到頁面上,若是程序編譯完以後,這個顯示過程基本是瞬時的。可是一樣的需求在Flask中,我竟然能夠感受到明顯的延遲(大概幾百毫秒的等待時間)。因此,若是你想寫一個比較快的Web程序,仍是用Java或者JVM語言吧,雖然看着土,性能確實槓槓的 。