欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的flask框架詳解

 更新時間:2022年02月08日 15:59:23   作者:一抹淺笑  
flask是一個基于Python開發(fā)并且依賴jinja2模板和Werkzeug?WSGI服務(wù)的一個微型框架,對于Werkzeug本質(zhì)是Socket服務(wù)端,其用于接收http請求并對請求進(jìn)行預(yù)處理,然后觸發(fā)Flask框架,本文給大家介紹Python中的flask框架,感興趣的朋友跟隨小編一起看看吧

Flask是一個Python編寫的Web 微框架,讓我們可以使用Python語言快速實現(xiàn)一個網(wǎng)站或Web服務(wù)。本文參考自Flask官方文檔,大部分代碼引用自官方文檔。

安裝flask

首先我們來安裝Flask。最簡單的辦法就是使用pip。

pip install flask

然后打開一個Python文件,輸入下面的內(nèi)容并運行該文件。然后訪問localhost:5000,我們應(yīng)當(dāng)可以看到瀏覽器上輸出了hello world。

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)

調(diào)試模式

我們修改代碼中的輸出,然后查看瀏覽器上是否有變化。如果你照做的話,可以看到什么變化都沒有。其實Flask內(nèi)置了調(diào)試模式,可以自動重載代碼并顯示調(diào)試信息。這需要我們開啟調(diào)試模式,方法很簡單,設(shè)置FLASK_DEBUG環(huán)境變量,并將值設(shè)置為1?;蛘咴O(shè)置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()

然后再次運行程序,會看到有這樣的輸出。這時候如果再次修改代碼,會發(fā)現(xiàn)這次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的話,應(yīng)該對路由很熟悉。路由通過使用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)選項,接受除了斜杠之外的字符串
int接受整數(shù)
float接受浮點數(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程序中常常需要獲取某個頁面的URL,在Flask中需要使用url_for('方法名')來構(gòu)造對應(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端點名和文件名。在下面的例子中,實際的文件應(yīng)放在static/文件夾下。

url_for('static', filename='style.css')

模板生成

Flask默認(rèn)使用Jinja2作為模板,F(xiàn)lask會自動配置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ù)配置了一個 Logger,我們可以直接在程序中使用。這個Logger是一個標(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')

處理請求

在 Flask 中獲取請求參數(shù)需要使用request等幾個全局對象,但是這幾個全局對象比較特殊,它們是 Context Locals ,其實就是 Web 上下文中局部變量的代理。雖然我們在程序中使用的是全局變量,但是對于每個請求作用域,它們都是互不相同的變量。理解了這一點,后面就非常簡單了。

Request 對象

Request 對象是一個全局對象,利用它的屬性和方法,我們可以方便的獲取從頁面?zhèn)鬟f過來的參數(shù)。

method屬性會返回HTTP方法的類似,例如postget。form屬性是一個字典,如果數(shù)據(jù)是POST類型的表單,就可以從form屬性中獲取。下面是 Flask 官方的例子,演示了 Request 對象的methodform屬性。

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屬性獲取,這個屬性也是一個字典。

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))

Cookies

Flask也可以方便的處理Cookie。使用方法很簡單,直接看官方的例子就行了。下面的例子是如何獲取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

重定向和錯誤

redirectabort函數(shù)用于重定向和返回錯誤頁面。

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)的錯誤頁面是一個空頁面,如果需要自定義錯誤頁面,可以使用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會根據(jù)函數(shù)的返回值自動決定如何處理響應(yīng):如果返回值是響應(yīng)對象,則直接傳遞給客戶端;如果返回值是字符串,那么就會將字符串轉(zhuǎn)換為合適的響應(yīng)對象。我們也可以自己決定如何設(shè)置響應(yīng)對象,方法也很簡單,使用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

我們可以使用全局對象session來管理用戶會話。Sesison 是建立在 Cookie 技術(shù)上的,不過在 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 模板的使用方法,詳細(xì)資料直接看原文檔吧。

模板標(biāo)簽

其實Jinja 模板和其他語言和框架的模板類似,反正都是通過某種語法將HTML文件中的特定元素替換為實際的值。如果使用過JSP、Thymeleaf 等模板,應(yīng)該可以非常容易的學(xué)會使用 Jinja模板。

其實從上面的例子中我們應(yīng)該可以看到Jinja 模板的基本語法了。代碼塊需要包含在{% %}塊中,例如下面的代碼。

{% extends 'layout.html' %}
{% block title %}主頁{% endblock %}
{% block body %}

    <div class="jumbotron">
        <h1>主頁</h1>
    </div>
{% endblock %}

雙大括號中的內(nèi)容不會被轉(zhuǎn)義,所有內(nèi)容都會原樣輸出,它常常和其他輔助函數(shù)一起使用。下面是一個例子。

<a class="navbar-brand" href={{ url_for('index') }}>Flask小例子</a>

繼承

模板可以繼承其他模板,我們可以將布局設(shè)置為父模板,讓其他模板繼承,這樣可以非常方便的控制整個程序的外觀。

例如這里有一個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') }}" rel="external nofollow" />
    <link rel="stylesheet" href="{{ url_for('static',filename='css/bootstrap-theme.css') }}" rel="external nofollow" />

</head>
<body>
<div class="container body-content">
    {% block body %}{% endblock %}
</div>
<div class="container footer">
    <hr>
    <p>這是頁腳</p>
<script src="{{ url_for('static',filename='js/jquery.js') }}"></script>
<script src="{{ url_for('static',filename='js/bootstrap.js') }}"></script>
</body>
</html>

其他模板可以這么寫。對比一下面向?qū)ο缶幊痰睦^承概念,我們可以很容易的理解。

{% extends 'layout.html' %}
{% block title %}主頁{% endblock %}
{% block body %}

    <div class="jumbotron">
        <h1>主頁</h1>
        <p>本項目演示了Flask的簡單使用方法,點擊導(dǎo)航欄上的菜單條查看具體功能。</p>
    </div>
{% endblock %}

控制流

條件判斷可以這么寫,類似于JSP標(biāo)簽中的Java 代碼,{% %}中也可以寫Python代碼。下面是Flask官方文檔的例子。

<div class=metanav>
  {% if not session.logged_in %}
    <a href="{{ url_for('login') }}" rel="external nofollow" >log in</a>
  {% else %}
    <a href="{{ url_for('logout') }}" rel="external nofollow" >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ù)注冊到模板中。可以參考這個爆棧提問。

寫在最后

這篇文章主要參考了Flask的官方文檔,但是只介紹了 Flask的最基本的一部分。了解了這部分,我們可以用Python 搭一個小服務(wù)器做點事情。如果希望詳細(xì)了解 Flask的使用用法,請關(guān)注更詳細(xì)的資料。本文就是起一個拋磚引玉的效果。

順便說,通過Flask 我也了解了Python 語言的執(zhí)行速度。我們都知道編譯器編譯出來的代碼執(zhí)行起來要比解釋器解釋代碼要快大約幾十倍到幾千倍不等。以前學(xué)Java的時候,感覺Java 慢,主要原因就是等待編譯時間比較長。相對來說用Python寫腳本就很塊了,因為沒有編譯過程。

但是從Flask的運行速度來看,我切身感受到了Python 執(zhí)行確實不快。舉個例子,在Spring中寫一個控制器,接受HTTP參數(shù),并顯示到頁面上,如果程序編譯完之后,這個顯示過程基本是瞬時的。但是同樣的需求在Flask中,我居然可以感覺到明顯的延遲(大概幾百毫秒的等待時間)。所以,如果你想寫一個比較快的Web程序,還是用Java或者JVM語言吧,雖然看著土,性能確實杠杠的 。

到此這篇關(guān)于Python中的flask框架詳解的文章就介紹到這了,更多相關(guān)Python flask框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論