pylang
pylang

Reputation: 44545

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

enter image description here

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

enter image description here

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.

Upvotes: 3

Views: 3168

Answers (3)

NotoriousPyro
NotoriousPyro

Reputation: 647

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

Upvotes: 1

frainfreeze
frainfreeze

Reputation: 577

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.

Upvotes: 0

pylang
pylang

Reputation: 44545

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.

Upvotes: 1

Related Questions