2
votes

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.

2
Have you tried setting all to : make env; make debug; flask run ? - JacobIRR
On windows the only way to get this to work as-is would be changing the recipes to setx FLASK_APP app.py etc. If this is the entirety of your makefile however you are using the wrong tool for the job, this should just be a batch file. As the first line in the make manual says: "The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them." - user657267
@JacobIRR I have tried this in the past. I get a No rule to make target 'env;. Stop.' - pylang
@user657267 Thanks. setx did not work. I eventually want to extend the file to work in bash terminals. I'll try a batch file. - pylang

2 Answers

1
votes

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.

0
votes

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.