DEV Community

Cover image for How to run the Flask app from CLI
Sachin
Sachin

Posted on • Originally published at geekpython.in

How to run the Flask app from CLI

Introduction

Flask is a micro web framework purely written in Python that allows us to build web applications. Flask is a lightweight framework that helps in web development using Python.

Some of you who have already worked with Flask know there are two ways to run the flask application.

app.run()- not the only option

After writing minimal code for our application, we use app.run() in our IDE or Code editor to run the Flask app.

It is an excellent and easy way to get started and see our app on the web, but as developers, we must learn new things for no one but ourselves to keep enhancing our experience.

flask run

Flask provides the flask run CLI command for running our applications from our terminals on Windows, macOS, and Linux.

We'll talk about how to run our Flask application from the command line.

I'm assuming you're familiar with Flask, but if you aren't, don't worry. In this tutorial, we'll start from scratch.

Creating virtual environments

We'll create two Python virtual environments for two versions of Flask. One is for Flask v2.2.x, and another is for Flask less than v2.2.x.

If you're wondering why we'll be working on two Flask versions. Then I must inform you that after Flask v2.2, new CLI commands were added to run the flask app from the terminal, and prior to Flask v2.2, we used different CLI commands. As a result, we'll see both scenarios.

Please create a new directory or make it using the mkdir command in your terminal and set up the virtual environments.

mkdir cli_flask
Enter fullscreen mode Exit fullscreen mode

I am using the virtualenv package for better performance and features.

cd cli_flask
virtualenv Flask_X

virtualenv Flask_Y
Enter fullscreen mode Exit fullscreen mode

Installing Flask

Activate your Flask_X application and install Flask v2.2.1.

# Activating Flask_X
.\Flask_X\Scripts\activate

# Installing Flask v2.2.1
(Flask_X)> pip install Flask==2.2.1
Enter fullscreen mode Exit fullscreen mode

Similarly, with the application Flask_Y, we'll activate and install Flask v1.1.4.

# Activating Flask_Y
.\Flask_Y\Scripts\activate

# Installing Flask v1.1.4
(Flask_Y)> pip install Flask==1.1.4
Enter fullscreen mode Exit fullscreen mode

For detailed knowledge of Python virtual environments. Click here

Creating Flask App

We need to write some code to make our minimal Flask app. Refer to the official site for a quick start guide to creating a minimal flask app in just a few lines of code.

from flask import Flask

app = Flask(__name__)

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

We'll add the same code inside the app.py file for both applications. Open your IDE or Code editor and make an app.py file inside the directory Flask_X. The same goes for Flask_Y.

Flask_X > app.py

from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello_world():
    return "<h1>Flask_X Application</h1> <h2>Hey, there Developers!</h2>"
Enter fullscreen mode Exit fullscreen mode

Flask_Y > app.py

from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello_world():
    return "<h1>Flask_Y Application</h1> <h2>Hey, there Geeks!</h2>"
Enter fullscreen mode Exit fullscreen mode

CLI commands

Primarily, we use the app.run() because it seems to be a more convenient way to start our Flask application, but it got some disadvantages too.

We will run our Flask application from our terminal, which is the recommended way by the Flask project.

Note: The CLI commands used in this tutorial are for Command Prompt(CMD) on Windows Operating System.

For the latest version

With the release of the latest versions of Flask, --app flag was added, which is used to specify how to load the application.

Running the development server

(Flask_X)> flask --app app run
Enter fullscreen mode Exit fullscreen mode

We used the --app and then specified our Flask application name to tell Flask that you have to run that specific application.

However, we can directly run the command flask run, and it will run successfully because it will try to import and detect the application instance as a .py file or package.

(Flask_X)> flask run
Enter fullscreen mode Exit fullscreen mode

If no instance is found, the command will look for a factory function named create_app or make_app that returns an instance.

WARNING: Do not use this command to run your application on the production server. Use the development server for development.

Debug mode

Do you realize how useful enabling debug mode when running the Flask app can be?

If we enable debug mode, we won't have to restart our server every time we make changes to the code because it will enable the reloader and interactive debugger by default, allowing us to easily read and see the errors.

--debug option is used to enable debug mode.

(Flask_X)> flask --app app --debug run

.........
 * Serving Flask app 'app'
 * Debug mode: on
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 584-527-312
Enter fullscreen mode Exit fullscreen mode

Handling environment variables

.env files are used to store highly confidential information which is not supposed to be shown publicly.

Flask does not load or read .env file when running the app by default. Hence Flask shows a warning.

Flask_X > app.py

from flask import Flask
import os

app = Flask(__name__)

env_file = os.environ.get("TEXT")

@app.route("/")
def hello_world():
    return f"<h1>{env_file}</h1>"
Enter fullscreen mode Exit fullscreen mode

When we try to run the Flask app from the cmd

(Flask_X)> flask --app app --debug run

.........
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
Enter fullscreen mode Exit fullscreen mode

To avoid the warning, install the python-dotenv package. After that, try to rerun the Flask app, and this time the Flask app will read the .env file.

Serving env file

Running on a different port

Use --port flag to run the Flask app on different port other than port=5000.

(Flask_X)> flask run --port 4000

.........
Running on http://127.0.0.1:4000
Press CTRL+C to quit
Enter fullscreen mode Exit fullscreen mode

For the older version

--app flag is unavailable for Flask less than v2.2.x. Instead we need to use FLASK_APP command.

Setting up

We need to set up our Flask app first to run it.

(Flask_Y)> set FLASK_APP=app
(Flask_Y)> flask run
Enter fullscreen mode Exit fullscreen mode

WARNING: Do not use this command to run your application on the production server. Use the development server for development.

Debug mode

To enable debug mode, we need to set the FLASK_DEBUG value to 1.

(Flask_Y)> set FLASK_DEBUG=1
(Flask_Y)> flask run

.........
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 151-573-460
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

Another way to enable debug mode by default is to set the FLASK_ENV value to development.

(Flask_Y)> set FLASK_ENV=development
(Flask_Y)> flask run

.........
 * Serving Flask app "app" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 151-573-460
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

Running on a different port

By default, the Flask app runs on the port=5000. To run our Flask app on port other than 5000 we have to set FLASK_RUN_PORT value to any port number.

(Flask_Y)> set FLASK_RUN_PORT=3001
(Flask_Y)> flask run

.........
Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

Conclusion

It's always been a difficult decision to run the Flask app through the app.run() or the flask run command. You can use either command, depending on your comfort or preference.

Both commands have nearly identical features, but there is one minor distinction. There is no command-line interface for app.run(). It does, however, directly invoke the run() method on the object, whereas the flask run command needs to learn where our Flask app instance is located, which it does by setting the FLASK_APP environment variable to point to it.

After all, in this tutorial, we've learned.

  • how to run the Flask app using the command-line interface.

  • how to run the Flask app on different ports.

  • how to handle .env files.

  • how to enable a debugger for easier viewing of errors.

Furthermore, we have a thorough understanding of how to create and activate Python virtual environments in Windows using a command prompt.


That's all for now

Keep Coding✌✌

Top comments (0)