简体   繁体   English

makefile 在调试模式下启动 Flask

[英]makefile to start Flask in debug mode

I would like to start Flask in debug/development mode using a single command.我想使用单个命令在调试/开发模式下启动Flask

Given鉴于

A fresh terminal, changing into the project directory, activating a virtual environment and running a custom Makefile :一个的终端,切换到项目目录,激活虚拟环境并运行自定义Makefile

> cd project
> activate myenv

(myenv) > make

Output Output

在此处输入图像描述

Debug mode is OFF.调试模式关闭。 However, running the commands separately turns it ON (as expected):但是,单独运行命令将其打开(如预期的那样):

(myenv) > set FLASK_APP=app.py
(myenv) > set FLASK_ENV=development
(myenv) > flask run

Output Output

在此处输入图像描述

Code代码

I've created the following Makefile , but when run, the debug mode does not turn on:我创建了以下Makefile ,但是在运行时,调试模式没有打开:

Makefile

all:
    make env && \
    make debug && \
    flask run

env:
    set FLASK_APP=app.py

debug:
    set FLASK_ENV=development

How do I improve the Makefile to run Flask in debug mode?如何改进Makefile以在调试模式下运行 Flask?

Note: instructions vary slightly for each operating system ;注意:每个操作系统的说明略有不同 at the moment, I am testing this in a Windows command prompt.目前,我正在 Windows 命令提示符下对此进行测试。

While I still believe a Makefile is a more general approach on other systems, I settled with @user657267's recommendation to use a batch file on Windows:虽然我仍然相信Makefile在其他系统上是一种更通用的方法,但我接受了 @user657267 的建议,即在 Windows 上使用批处理文件:

Code代码

# start_flask.bat
:: set environment varibles (app and debug mode)
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
pause

Demo演示

> start_flask.bat

Output输出

set FLASK_APP=app.py

set FLASK_ENV=development

flask run
 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * ...
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

I am willing accept another solution.我愿意接受另一种解决方案。

Makefiles should be deterministic. Makefile 应该是确定性的。 Having one command which could toggle between the two is not the best way to do it.拥有一个可以在两者之间切换的命令并不是最好的方法。

Simply create your makefile like so:只需像这样创建您的 makefile:

FLASK_APP = app.py
FLASK := FLASK_APP=$(FLASK_APP) env/bin/flask

.PHONY: run
run:
    FLASK_ENV=development $(FLASK) run

.PHONY: run-production
run-production:
    FLASK_ENV=production $(FLASK) run

Now you can just do现在你可以做

make run

or或者

make run-production

Alternatively, on Linux inside of a makefile you can place everything on a single line without export keyword.或者,在 Linux 上的 makefile 中,您可以将所有内容放在一行中,而无需export关键字。

debug:
    FLASK_APP=app.py FLASK_ENV=development flask run

As per flask docs .根据烧瓶文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM