Python中的flask框架详解
flask是一个python编写的web 微框架,让我们可以使用python语言快速实现一个网站或web服务。本文参考自flask官方文档,大部分代码引用自官方文档。
安装flask
首先我们来安装flask。最简单的办法就是使用pip。
pip install flask
然后打开一个python文件,输入下面的内容并运行该文件。然后访问localhost:5000
,我们应当可以看到浏览器上输出了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)
调试模式
我们修改代码中的输出,然后查看浏览器上是否有变化。如果你照做的话,可以看到什么变化都没有。其实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会自动重启。
* 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的注解。
@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
构造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
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()
静态文件
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 对象
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))
cookies
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
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'
模板简介
这里简单的介绍一下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') }}" 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>
其他模板可以这么写。对比一下面向对象编程的继承概念,我们可以很容易的理解。
{% 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') }}" rel="external nofollow" >log in</a> {% else %} <a href="{{ url_for('logout') }}" rel="external nofollow" >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语言吧,虽然看着土,性能确实杠杠的 。
到此这篇关于python中的flask框架详解的文章就介绍到这了,更多相关python flask框架内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!