Flask是一个使用 Python 编写的轻量级 Web 应用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 ,使用 BSD 授权。
快速入门 quick start
随便找个目录吧
创建一个文件:hello.py ,内容如下:
Flask也被称为 “microframework” ,因为它使用简单的核心,用 extension 增加其他功能。Flask没有默认使用的数据库、窗体验证工具。然而,Flask保留了扩增的弹性,可以用Flask-extension加入这些功能:ORM、窗体验证工具、文件上传、各种开放式身份验证技术。最新版本为0.12。
学习环境
macOS sierra 10.12.3
Python 2.7.10
pip 9.0.1
Flask-0.12.2
安装Flask
Linux或mac下使用以下命令安装:
sudo pip install Flask输出如下:
$ sudo pip install Flask Password: The directory '/Users/aven/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/aven/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting Flask Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB) 100% |████████████████████████████████| 92kB 90kB/s Collecting itsdangerous>=0.21 (from Flask) Downloading itsdangerous-0.24.tar.gz (46kB) 100% |████████████████████████████████| 51kB 24kB/s Collecting Werkzeug>=0.7 (from Flask) Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB) 100% |████████████████████████████████| 317kB 8.0kB/s Collecting Jinja2>=2.4 (from Flask) Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB) 100% |████████████████████████████████| 348kB 8.8kB/s Collecting click>=2.0 (from Flask) Downloading click-6.7-py2.py3-none-any.whl (71kB) 100% |████████████████████████████████| 71kB 11kB/s Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask) Downloading MarkupSafe-1.0.tar.gz Installing collected packages: itsdangerous, Werkzeug, MarkupSafe, Jinja2, click, Flask Running setup.py install for itsdangerous ... done Running setup.py install for MarkupSafe ... done Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 itsdangerous-0.24执行 flask 查看是否安装成功:
$ flask Usage: flask [OPTIONS] COMMAND [ARGS]... This shell command acts as general utility script for Flask applications. It loads the application configured (through the FLASK_APP environment variable) and then provides commands either provided by the application or Flask itself. The most useful commands are the "run" and "shell" command. Example usage: $ export FLASK_APP=hello.py $ export FLASK_DEBUG=1 $ flask run Options: --version Show the flask version --help Show this message and exit. Commands: run Runs a development server. shell Runs a shell in the app context.完了,这就装完了,比 Django 好,环境变量都不需要配置。
快速入门 quick start
随便找个目录吧
创建一个文件:hello.py ,内容如下:
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello, World' @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 @app.route('/buy', methods=['POST']) def buy(): stripe_token = request.form['stripeToken'] if __name__ == "__main__": app.run()打开终端,切换到文件所在的目录,执行以下命令启动服务:
$ export FLASK_APP=hello.py $ export FLASK_DEBUG=1 $ flask run打开浏览器访问吧: http://127.0.0.1:5000/
* Serving Flask app "hello" * Forcing debug mode on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 532-211-424 127.0.0.1 - - [03/Nov/2017 11:23:03] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [03/Nov/2017 11:23:04] "GET /favicon.ico HTTP/1.1" 404 - * Detected change in '/Users/aven/Documents/flask/hello.py', reloading * Restarting with stat * Debugger is active! * Debugger PIN: 532-211-424 127.0.0.1 - - [03/Nov/2017 11:27:26] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [03/Nov/2017 11:27:28] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [03/Nov/2017 11:45:16] "GET /hello HTTP/1.1" 200 - 127.0.0.1 - - [03/Nov/2017 11:46:12] "GET /user/%E5%BE%AEwx%E7%AC%91 HTTP/1.1" 200 - 127.0.0.1 - - [03/Nov/2017 13:33:14] "GET /user/%E5%BE%AEwx%E7%AC%91a HTTP/1.1" 200 -
命令说明:
1、设置APP 的启动文件;
2、设置为调试模式启动;
3、运行;
如果你使用