Flask入门教程务器,我们会在最后一章学习。 第 2 章:Hello, Flask! 15 解剖时间 下面我们来分解这个 Flask 程序,了解它的基本构成。 首先我们从 flask 包导入 Flask 类,通过实例化这个类,创建一个程序对象 app : from flask import Flask app = Flask(__name__) 接下来,我们要注册一个处理函数,这个函数是处理某个请求的处理函数,Flask 或 wsgi.py 的文件中。如果你使用了其他名称,就要设置系统环境变量 FLASK_APP 来告诉 Flask 你要启动哪个程序。 Flask 通过读取这个文件对应的模块寻找要运行的程序实例,你可以把它设置成下 面这些值: 模块名 Python 导入路径 文件目录路径 管理环境变量 现在在启动 Flask 程序的时候,我们通常要和两个环境变量打交道: FLASK_APP HTML 或其他格式的文本叫做模板,执行这些变量替 换和逻辑计算工作的过程被称为渲染,这个工作由我们这一章要学习使用的模板渲 染引擎——Jinja2 来完成。 按照默认的设置,Flask 会从程序实例所在模块同级目录的 templates 文件夹中寻 找模板,我们的程序目前存储在项目根目录的 app.py 文件里,所以我们要在项目 根目录创建这个文件夹: $ mkdir templates0 码力 | 127 页 | 7.62 MB | 1 年前3
Flask Documentation (1.1.x)adds two numbers together and returns the result. We configure Celery’s broker and backend to use Redis, create a celery application using the factor from above, and then use it to define the task. from flask_app = Flask(__name__) flask_app.config.update( CELERY_BROKER_URL='redis://localhost:6379', CELERY_RESULT_BACKEND='redis://localhost:6379' ) celery = make_celery(flask_app) @celery.task() def0 码力 | 428 页 | 895.98 KB | 1 年前3
Flask Documentation (1.1.x)adds two numbers together and returns the result. We configure Celery’s broker and backend to use Redis, create a celery application using the factor from above, and then use it to define the task. from CELERY_BROKER_URL='redis://localhost:6379', (continues on next page) 154 Chapter 1. User’s Guide Flask Documentation (1.1.x), Release 1.1.4 (continued from previous page) CELERY_RESULT_BACKEND='redis://localhost:6379'0 码力 | 291 页 | 1.25 MB | 1 年前3
共 3 条
- 1













