简体   繁体   中英

makefile to start Flask in debug mode

I would like to start Flask in debug/development mode using a single command.

Given

A fresh terminal, changing into the project directory, activating a virtual environment and running a custom Makefile :

> cd project
> activate myenv

(myenv) > make

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

在此处输入图像描述

Code

I've created the following Makefile , but when run, the debug mode does not turn on:

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?

Note: instructions vary slightly for each operating system ; at the moment, I am testing this in a Windows command prompt.

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:

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. Having one command which could toggle between the two is not the best way to do it.

Simply create your makefile like so:

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.

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

As per flask docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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