A Handy Script for `flask run`

by Richard 2022-02-15 12:54


You have just made a Flask project. You've made your basic (aka "minimal") app. It looks a little something like this:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

Great. Time to start your server. Go to your terminal, and at the prompt you enter:

$ export FLASK_APP=<app_name>.py
$ flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Production? D'oh! Right. You don't want to be in the production environment. It's OK. You don't really start projects on the reg. It's easy to forget all the steps. Hit your Ctrl + C and then, at the prompt:

$ export FLASK_ENV=development
$ flask run
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 999-999-999

Development environment. That's better. Now you do your work. You close the project. You grab a beer, you watch some TV, you play a game, you go to bed.

The next day...

$ flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Production?!? Damn, that's annoying! Now you've got to do that export FLASK_ENV=development thing again.

There are loads of ways to deal with this. You could make a config.py. You could put the environment variables in an .env file and use the python-dotenv package. One tip I've picked up that I like (noob that I am) is to use an executable script. It's super-easy as you are getting going so you don't have to be exasperated by exporting environment variables. Hell, you don't even have to take the time to write flask run.

Born to ./run

Make a file in the root directory of your project. I call it run and give it no extension. Open this blank file in your IDE. Here's your code: FLASK_APP=run.py FLASK_ENV=development flask run. All one line.

Great, that's a fantastic bit of writing. It does nothing right now. Do this thing. In the terminal. The Bash terminal:

$ chmod +x run

So we just made a file and made it...well, we made it executable. That's dangerous, of course. Just had to say it.

But now, now we have some pretty wicked power file stuff happening. We got run. You can quit your project. You can log off. You can shut down, restart, all the stuff.

When we come back to it, get to your directory. Activate your virtual environment. Then, type ./run.

WHAT?!?

 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 999-999-999

That's cool. You're cool. No need for flask run from here. Go rock it!

Go Back

Leave a comment

Comments 0