#flask介绍#
Flask是一个基于python的,微型web框架。之所以被称为微型是因为其核心非常简单,同时具有很强的扩展能力。它几乎不给使用者做任何技术决定。
安装flask时应该注意其必须的几个支持包比如Jinja2,Werkzeug等。如果使用easy_install或者pip这样的安装工具的话那么就不必担心这么多了。另外flask还有一些可选的辅助模块,使用它们可以让程序更加简洁易懂,比如SQLAlchemy用于数据库的连接和操作,flask-WTForm用于网页上表单的设计。
最简单的一个flask的web应用表格如下:
.py 里的内容
from flask import Flask,render_template
app = Flask(name)
@app.route(’/’)
def index():
books = [
{
‘name’: u’红楼梦’,
‘author’: u’曹雪芹’,
‘price’: 200
},
{
‘name’: u’水浒传’,
‘author’: u’施耐庵’,
‘price’: 100
},
{
‘name’: u’三国演义’,
‘author’: u’罗贯中’,
‘price’: 120
},
{
‘name’: u’西游记’,
‘author’: u’吴承恩’,
‘price’: 230
}
]
return render_template(‘index.html’, books=books)
if name == ‘main’:
app.run(debug=True)
#html里的内容:#
<table >
<tbody>
{% for book in books %}
<tr bgcolor="#7fffd4">
<td >{{ book.name}}</td>
<td >{{ book.author}}</td>
<td >{{ book.price}}</td>
</tr>
{% endfor %}
</tbody>
</table>
输出结果: