You are currently viewing How To Run Flask App From The Command Line In Windows

How To Run Flask App From The Command Line In Windows

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.

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

Installing Flask

Activate your Flask_X application and install Flask v2.2.1.

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

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.

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

Flask_Y > app.py

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

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.

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.

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

When we try to run the Flask app from the cmd

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.

Environment variable serving on Flask app

Running on a different port

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

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.

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.

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

Running on a different port

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

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✌✌