Command Line Interface

Installing Flask installs the flask script, a Click command line interface, in your virtualenv. Executed from the terminal, this script gives access to built-in, extension, and application-defined commands. The --help option will give more information about any commands and options.

Application Discovery

The flask command is installed by Flask, not your application; it must be told where to find your application in order to use it. The FLASK_APP environment variable is used to specify how to load the application.

Unix Bash (Linux, Mac, etc.):

$ export FLASK_APP=hello
$ flask run

FLASK_APP has three parts: an optional path that sets the current working directory, a Python file or dotted import path, and an optional variable name of the instance or factory. If the name is a factory, it can optionally be followed by arguments in parentheses. The following values demonstrate these parts:

FLASK_APP=src/hello
Sets the current working directory to src then imports hello.
FLASK_APP=hello.web
Imports the path hello.web.
FLASK_APP=hello:app2
Uses the app2 Flask instance in hello.
FLASK_APP="hello:create_app('dev')"
The create_app factory in hello is called with the string 'dev' as the argument.

If FLASK_APP is not set, the command will look for a file called wsgi.py or app.py and try to detect an application instance or factory.

The environment in which the Flask app runs is set by the FLASK_ENV environment variable. If not set it defaults to production
If the env is set to development, the flask command will enable debug mode and flask run will enable the interactive debugger and reloader.
If you want to control debug mode separately, use FLASK_DEBUG. The value 1 enables it, 0 disables it.

To explore the data in your application, you can start an interactive Python shell with the shell command. An application context will be active, and the app instance will be imported.

$ flask shell
Python 3.6.2 (default, Jul 20 2017, 03:52:27)
[GCC 7.1.1 20170630] on linux
App: example
Instance: /home/user/Projects/hello/instance
>>>

Use shell_context_processor() to add other automatic imports.

you can use Flask’s dotenv support to set environment variables automatically.

If python-dotenv is installed, running the flask command will set environment variables defined in the files .env and .flaskenv.

This can be used to avoid having to set FLASK_APP manually every time you open a new terminal.

Variables set on the command line are used over those set in .env, which are used over those set in .flaskenv.

.flaskenv should be used for public variables, such as FLASK_APP, while .env should not be committed to your repository so that it can set private variables

The files are only loaded by the flask command or calling run(). If you would like to load these files when running in production, you should call load_dotenv() manually.

Click is configured to load default values for command options from environment variables. The variables use the pattern FLASK_COMMAND_OPTION. For example, to set the port for the run command, instead of flask run --port 8000:

export FLASK_RUN_PORT=8000
flask run
* Running on http://127.0.0.1:8000/

These can be added to the .flaskenv file just like FLASK_APP to control default command options.

You can tell Flask not to load dotenv files even when python-dotenv is installed by setting the FLASK_SKIP_DOTENV environment variable.

export FLASK_SKIP_DOTENV=1
flask run

运行flask程序的更多相关文章

  1. flask 程序结构概括

    以此结构为例,这个小项目是<Flask Web开发:基于python的web应用开发实战>第一部分结束后的代码框架 第一层 有app.tests.migrations三个文件夹和confi ...

  2. 一个简单的flask程序

    初始化 所有Flask程序都必须创建一个程序实例. 程序实例是Flask类的对象,经常使用下述代码创建: from flask import Flask app = Flask(__name__) F ...

  3. windows环境隐藏命令行窗口运行Flask项目

    Linux下可以使用nohub来使Flask项目在后台运行,而windows环境下没有nohub命令,如何让Flask项目在windows中在后台运行而不显示命令行窗口呢? 1.写一个.bat脚本来启 ...

  4. Flask从入门到精通之flask程序入门

    初始化 所有Flask程序都必须创建一个程序实例,Web服务器使用一种名为Web服务器网关接口的的协议(WSGI),把接收自客户端的所有请求转发给这个对象处理.程序实例是Flask类的对象,使用下面代 ...

  5. Flask 程序的基本结构

    1.初始化 所有Flask程序都必须创建一个程序实例.web服务器使用一种名为Web服务器网关借口的协议,把接收自客户端的所有请求都转交给这个对象处理. from flask import Flask ...

  6. 第一个flask程序

    flask简介: flask是一款非常流行的Python Web框架,出生于2010年,作者是Armin  Ronacher,本来这个项目只是作者在愚人节的一个玩笑,后来由于非常受欢迎,进而成为一个正 ...

  7. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  8. 运行python程序

    1 在windows下运行python程序 1)从DOS命令行运行python脚本 用python解释器来执行python脚本,在windows下面python解释器是python.exe,我的pyt ...

  9. windows批处理运行java程序

    明确需求 今天你编了一个java swing版照片查看器,想让计算机上的所有照片默认打开方式都改成你的照片查看器. 使用工具软件 很多工具软件都是不把jre打包到exe中的,这就是说打包之后的exe只 ...

随机推荐

  1. LVS/NAT 配置

    LVS/NAT 配置 实验环境 三台主机:Linux Centos 6.4 32位 调度器Director:192.168.1.160(内网IP).192.168.2.20(公网IP) HTTP真实服 ...

  2. iOS如何获取蓝牙Mac地址

    http://macpu.github.io/2015/11/12/iOS%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E8%93%9D%E7%89%99Mac%E5%9C ...

  3. 使用Navicat连接oracle时出现unsupported server character set ZHS16GBK的解决之道

    原文网址http://blog.mn886.net/chenjianhua/show/ba1dc6f835be403ea159b0a5e2685ff2/index.html ORA-12737:Ins ...

  4. 【atcoder】All Your Paths are Different Lengths[arc102D](乱搞)

    题目传送门:https://arc102.contest.atcoder.jp/tasks/arc102_b 这道题有点毒瘤啊,罚时上天.. 显然若$ l=2^n $那么就可以直接二进制拆分,但是如果 ...

  5. window 安装 thrift

    1.下载thrift:http://thrift.apache.org/ 2.然后将该执行文件,保存到磁盘的文件夹下(你自己喜欢的,随便一个个目录下) 我的是(目录名少了个t,无妨): 3.把该执行文 ...

  6. DATEDIFF 的用法

    DECLARE @date DATETIME = '2017-12-26 00:00:00';DECLARE @date2 DATETIME = DATEADD(DAY, 1, @date);DECL ...

  7. 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举

    1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...

  8. 魔术师发牌问题--java实现

    package com.wyl.linklist; /** **问题名称:魔术师发牌问题 *问题描述:魔术师手里一共有13张牌,全是黑桃,1~13. *********魔术师需要实现一个魔术:这是十三 ...

  9. mysql数据库优化课程---5、要索引和不要索引的区别是什么

    mysql数据库优化课程---5.要索引和不要索引的区别是什么 一.总结 一句话总结: 索引速度快,就是查表的时候,操作的话设置索引就好了 1.数据库设计的时候不允许字段为null的好处是什么? nu ...

  10. spring:自定义限定符注解@interface, 首选bean

    spring:自定义限定符注解@interface, 首选bean 1.首选bean 在声明bean的时候,通过将其中一个可选的bean设置为首选(primary)bean能够避免自动装配时的歧义性. ...