在python web框架的世界里充满了选择。有Django,Flask,Pyramid,Tornado,Bottle,Diesel,Pecan,Falcon等等的来吸引开发者的注意。作为一个开发者,你想要从中选择一个框架来帮你完成项目,并且能继续做大事情。 关于读者该选择哪个框架,这里不做选择,本文带大家入门flask框架。 安装Flask
完整的flask开发环境可能需要内容组件比较多, 这里可以先安装下面的组件
然后直接安装 hello world打开一个Python文件,输入下面的内容并运行该文件。然后访问localhost:5000,我们应当可以看到浏览器上输出了Hello Flask!。
app初始化简介
运行测试app程序
app的配置参数详解flask实例化后会加载默认的配置参数,我们也可以手动设置参数更新默认的配置,常用的参数选项有:
通过加载文件设置参数
通过类设置参数
通过json格式的文件配置
直接操作app.config对象进行设置
获取配置参数的方法
路由路由通过使用Flask的app.route装饰器来设置,这类似Java的Spring Web MVC
route装饰器会将其装饰的视图函数注册到app的视图函数集中,其主要有三个参数:
|
转换器 | 作用 |
---|---|
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
@app.route('/loginto')
def login():
print(url_for('login')) # 会打印出网址中主机名后的部分
return 'Hello world!'
使用route装饰器的methods参数可以设置接收get或者post方法
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
print(request.form['userid']) # 获取post穿过来的参数
dict = request.form.to_dict() # 将请求参数解析成字典
print(dict['userid'])
return 'POST'
else:
print(request.args['userid']) # 获取get传过来的参数
dict = request.args.to_dict() # 将请求参数解析成字典
print(dict['userid'])
return 'GET'
利用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))
返回字符串、元组等可以直接返回。
返回字典使用
from flask import jsonify
@app.route('/test', methods=['GET', 'POST'])
def test():
dict={'a':'a','b':'aaa'}
return jsonify(dict)
返回模板
from flask import render_template
@app.route('/test', methods=['GET', 'POST'])
def test():
return render_template('index.html',name='aaa') # 可以向模板传递参数
Web程序中常常需要处理静态文件,在Flask中需要使用url_for函数并指定static端点名和文件名。在上面的例子中url_for可以获取函数名对应的网址。下面的例子,url_for是写在html模板中的,实际的文件应是static/logo.png文件。
h1 { margin: 0 0 30px 0; background: url({{ url_for('static', filename='logo.png') }}) }
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那样配置它。
app.logger.debug('A value for debugging')
app.logger.warning('A warning occurred (%d apples)', 42)
app.logger.error('An error occurred')
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 我也了解了Python 语言的执行速度。我们都知道编译器编译出来的代码执行起来要比解释器解释代码要快大约几十倍到几千倍不等。以前学Java的时候,感觉Java 慢,主要原因就是等待编译时间比较长。相对来说用Python写脚本就很块了,因为没有编译过程。
但是从Flask的运行速度来看,我切身感受到了Python 执行确实不快。举个例子,在Spring中写一个控制器,接受HTTP参数,并显示到页面上,如果程序编译完之后,这个显示过程基本是瞬时的。但是同样的需求在Flask中,我居然可以感觉到明显的延迟(大概几百毫秒的等待时间)。所以,如果你想写一个比较快的Web程序,还是用Java或者JVM语言吧,虽然看着土,性能确实杠杠的 。
|