Differences between revisions 1 and 2
Revision 1 as of 2013-02-22 21:23:47
Size: 4495
Editor: twotwo
Comment:
Revision 2 as of 2019-12-10 01:52:22
Size: 5288
Editor: twotwo
Comment: add useful extensions
Deletions are marked like this. Additions are marked like this.
Line 92: Line 92:
== User’s Guide ==

=
== Extensions ===
== Extensions ==
Line 97: Line 95:
==== Finding Extensions ==== === Finding Extensions ===
Line 100: Line 98:
==== Using Extensions ==== === Using Extensions ===
Line 115: Line 113:
==== Building Extensions ==== === Building Extensions ===
Line 117: Line 115:

=== Extensions ===
[[https://github.com/humiaozuzu/awesome-flask||target="_blank"]]
 * [[pytest]] pytest & pyest-cov
 * [[SQLAlchemy]]
 * [[https://github.com/agronholm/apscheduler|APScheduler|target="_blank"]]
  . [[https://medium.com/better-programming/introduction-to-apscheduler-86337f3bb4a6|Introduction to APScheduler|target="_blank"]]
  . [[https://apscheduler.readthedocs.io/||target="_blank"]]
 * [[https://flask-wtf.readthedocs.io/en/stable/|Flask-WTF|target="_blank"]] & [[https://wtforms-components.readthedocs.io/en/latest/|WTForms-Components|target="_blank"]]
 * [[https://flask-login.readthedocs.io/en/latest/|Flask-Login|target="_blank"]]
 * [[||target="_blank"]]

== User’s Guide ==
[[http://pycoder.net/bospy/presentation.html|Creating beautiful REST APIs with Flask|target="_blank"]]

Back to Python#Web_Applications

See Also Jinja2Werkzeuggunicorn

Flask

Flask是一个使用Python编写的轻量级Web应用框架。基于Werkzeug WSGI工具箱和Jinja2 模板引擎。Flask使用BSD授权。

Flask被称为“microframework”,因为它使用简单的核心,用extension增加其他功能。Flask没有默认使用的数据库、窗体验证工具。然而,Flask保留了扩增的弹性,可以用Flask-extension加入这些功能:ORM、窗体验证工具、文件上传、各种开放式身份验证技术。

1. History

2004年,一群来自世界各地的Python热衷者组成了Pocoo。

Flask的作者是来自Pocoo的Armin Ronacher。本来只是作者的一个愚人节玩笑,不过后来大受欢迎,进而成为一个正式的项目。Flask受Ruby的Sinatra影响。

2. Components

基于Pocoo项目Werkzeug和Jinja2。

2.1. Werkzeug

Werkzeug是一个用于Python编程语言的实用程序库,换句话说,它是一个用于Web服务器网关接口(WSGI)应用程序的工具包,并且是在BSD许可下授权的。Werkzeug可以实现用于请求、响应和实用功能的软件对象。它可用于在其之上构建自定义软件框架,并支持Python 2.6、2.7。

2.2. Jinja

Jinja也是由Ronacher开发的,它是Python编程语言的模板引擎,并且是在BSD许可下授权的。与Django web框架类似,它提供在沙箱中评估模板。

3. Get Started

pip install Flask

Running on http://127.0.0.1:5000/

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

3.1. Deployment

##启动端口
python web_console.py 8800

Run On http://host/context

        location /console {
            rewrite    /context/(.*) /$1 break;
            proxy_pass  http://localhost:8800/;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP       $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

3.2. Standalone WSGI Containers

https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/

# -*- coding: utf-8 -*-
# web.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

# wsgi.py
from web import app

if __name__ == "__main__":
    app.run()

pip install flask gunicorn
gunicorn --bind 0.0.0.0:5000 wsgi:app
$ gunicorn --bind unix:///srv/www/moin/var/gunicorn.sock wsgi:app
[2019-09-15 15:57:03 +0800] [39194] [INFO] Starting gunicorn 19.9.0
[2019-09-15 15:57:03 +0800] [39194] [INFO] Listening at: unix:/srv/www/moin/var/gunicorn.sock (39194)
[2019-09-15 15:57:03 +0800] [39194] [INFO] Using worker: sync
[2019-09-15 15:57:03 +0800] [39197] [INFO] Booting worker with pid: 39197

4. Extensions

扩展是向Flask应用程序添加功能的额外包。例如,扩展可能会添加对发送电子邮件或连接到数据库的支持。一些扩展添加了全新的框架来帮助构建特定类型的应用程序,比如REST API。

4.1. Finding Extensions

Framework :: Flask

4.2. Using Extensions

from flask_foo import Foo

foo = Foo()

app = Flask(__name__)
app.config.update(
    FOO_BAR='baz',
    FOO_SPAM='eggs',
)

foo.init_app(app)

4.3. Building Extensions

Flask Extension Development

4.4. Extensions

https://github.com/humiaozuzu/awesome-flask

5. User’s Guide

Creating beautiful REST APIs with Flask

5.1. Command Line Interface

安装 Flask 将在 virtualenv 中安装 Flask 脚本和一个单击命令行界面。该脚本从终端执行,允许访问内置、扩展和应用程序定义的命令。--help 选项将提供关于任何命令和选项的更多信息。

6. Reference

MainWiki: Flask (last edited 2019-12-10 01:52:22 by twotwo)