• FLASK 使用方法


    Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务。本文参考自Flask官方文档,大部分代码引用自官方文档。

    安装Flask

    首先我们来安装Flask。最简单的办法就是使用pip。

    pip install flask
    • 1

    然后打开一个Python文件,输入下面的内容并运行该文件。然后访问localhost:5000,我们应当可以看到浏览器上输出了Hello Flask!

    from flask import Flask
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def hello_world():
        return 'Hello Flask!'
    
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    快速开始

    调试模式

    我们修改代码中的输出,然后查看浏览器上是否有变化。如果你照做的话,可以看到什么变化都没有。其实Flask内置了调试模式,可以自动重载代码并显示调试信息。这需要我们开启调试模式,方法很简单,设置FLASK_DEBUG环境变量,并将值设置为1

    然后再次运行程序,会看到有这样的输出。这时候如果再次修改代码,会发现这次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)
    • 1
    • 2
    • 3
    • 4

    路由

    在上面的例子里可以看到路由的使用。如果了解Spring Web MVC的话,应该对路由很熟悉。路由通过使用Flask的app.route装饰器来设置,这类似Java的注解。

    @app.route('/')
    def index():
        return 'Index Page'
    
    @app.route('/hello')
    def hello():
        return 'Hello, World'
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    路径变量

    如果希望获取/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
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    构造URL

    在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
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    HTTP方法

    如果需要处理具体的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()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    静态文件

    Web程序中常常需要处理静态文件,在Flask中需要使用url_for函数并指定static端点名和文件名。在下面的例子中,实际的文件应放在static/文件夹下。

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

    模板生成

    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)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    相应的模板文件如下。

    <!doctype html>
    <title>Hello from Flask</title>
    {% if name %}
      <h1>Hello {{ name }}!</h1>
    {% else %}
      <h1>Hello, World!</h1>
    {% endif %}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    日志输出

    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')
    • 1
    • 2
    • 3

    处理请求

    在 Flask 中获取请求参数需要使用request等几个全局对象,但是这几个全局对象比较特殊,它们是 Context Locals ,其实就是 Web 上下文中局部变量的代理。虽然我们在程序中使用的是全局变量,但是对于每个请求作用域,它们都是互不相同的变量。理解了这一点,后面就非常简单了。

    Request 对象

    Request 对象是一个全局对象,利用它的属性和方法,我们可以方便的获取从页面传递过来的参数。

    method属性会返回HTTP方法的类似,例如postgetform属性是一个字典,如果数据是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)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    如果数据是由GET方法传送过来的,可以使用args属性获取,这个属性也是一个字典。

    searchword = request.args.get('key', '')
    • 1

    文件上传

    利用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))
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Cookies

    Flask也可以方便的处理Cookie。使用方法很简单,直接看官方的例子就行了。下面的例子是如何获取cookie。

    from flask import request
    
    @app.route('/')
    def index():
        username = request.cookies.get('username')
        # 使用 cookies.get(key) 代替 cookies[key] 避免
        # 得到 KeyError 如果cookie不存在
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果需要发送cookie给客户端,参考下面的例子。

    from flask import make_response
    
    @app.route('/')
    def index():
        resp = make_response(render_template(...))
        resp.set_cookie('username', 'the username')
        return resp
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    重定向和错误

    redirectabort函数用于重定向和返回错误页面。

    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()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    默认的错误页面是一个空页面,如果需要自定义错误页面,可以使用errorhandler装饰器。

    from flask import render_template
    
    @app.errorhandler(404)
    def page_not_found(error):
        return render_template('page_not_found.html'), 404
    • 1
    • 2
    • 3
    • 4
    • 5

    响应处理

    默认情况下,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
    • 1
    • 2
    • 3
    • 4
    • 5

    Sessions

    我们可以使用全局对象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'
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    模板简介

    这里简单的介绍一下Jinja 模板的使用方法,详细资料直接看原文档吧。

    模板标签

    其实Jinja 模板和其他语言和框架的模板类似,反正都是通过某种语法将HTML文件中的特定元素替换为实际的值。如果使用过JSP、Thymeleaf 等模板,应该可以非常容易的学会使用 Jinja模板。

    其实从上面的例子中我们应该可以看到Jinja 模板的基本语法了。代码块需要包含在{% %}块中,例如下面的代码。

    {% extends 'layout.html' %}
    {% block title %}主页{% endblock %}
    {% block body %}
    
        <div class="jumbotron">
            <h1>主页</h1>
        </div>
    
    {% endblock %}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    双大括号中的内容不会被转义,所有内容都会原样输出,它常常和其他辅助函数一起使用。下面是一个例子。

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

    继承

    模板可以继承其他模板,我们可以将布局设置为父模板,让其他模板继承,这样可以非常方便的控制整个程序的外观。

    例如这里有一个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>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    其他模板可以这么写。对比一下面向对象编程的继承概念,我们可以很容易的理解。

    {% extends 'layout.html' %}
    {% block title %}主页{% endblock %}
    {% block body %}
    
        <div class="jumbotron">
            <h1>主页</h1>
            <p>本项目演示了Flask的简单使用方法,点击导航栏上的菜单条查看具体功能。</p>
        </div>
    
    {% endblock %}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    控制流

    条件判断可以这么写,类似于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>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    循环的话可以这么写,和在Python中遍历差不多。

            <tbody>
            {% for key,value in data.items() %}
                <tr>
                    <td>{{ key }}</td>
                    <td>{{ value }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td>文件</td>
                <td></td>
            </tr>
            </tbody>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    需要注意不是所有的Python代码都可以写在模板里,如果希望从模板中引用其他文件的函数,需要显式将函数注册到模板中。可以参考这个爆栈提问

    写在最后

    这篇文章主要参考了Flask的官方文档,但是只介绍了 Flask的最基本的一部分。了解了这部分,我们可以用Python 搭一个小服务器做点事情。如果希望详细了解 Flask的使用用法,请关注更详细的资料。本文就是起一个抛砖引玉的效果。

    顺便说,通过Flask 我也了解了Python 语言的执行速度。我们都知道编译器编译出来的代码执行起来要比解释器解释代码要快大约几十倍到几千倍不等。以前学Java的时候,感觉Java 慢,主要原因就是等待编译时间比较长。相对来说用Python写脚本就很块了,因为没有编译过程。

    但是从Flask的运行速度来看,我切身感受到了Python 执行确实不快。举个例子,在Spring中写一个控制器,接受HTTP参数,并显示到页面上,如果程序编译完之后,这个显示过程基本是瞬时的。但是同样的需求在Flask中,我居然可以感觉到明显的延迟(大概几百毫秒的等待时间)。所以,如果你想写一个比较快的Web程序,还是用Java或者JVM语言吧,虽然看着土,性能确实杠杠的 。

    最后,我写了一个小练习,试了试Flask 的基本功能,如果有兴趣可以上我的Github查看代码。

  • 相关阅读:
    EntityFramework之领域驱动设计实践(二)
    博客园开始对X++语言语法高亮的支持
    一种来源于Microsoft Dynamics AX的权限管理设计思想
    使用InternalsVisibleToAttribute控制internal成员的访问
    EntityFramework之领域驱动设计实践:总结
    EntityFramework之领域驱动设计实践(九)
    EntityFramework之领域驱动设计实践(三)
    EntityFramework之领域驱动设计实践(七)
    EntityFramework之领域驱动设计实践(五)
    EntityFramework之领域驱动设计实践 (一)
  • 原文地址:https://www.cnblogs.com/klb561/p/8679320.html
Copyright © 2020-2023  润新知