"Run Flask App: A Comprehensive Guide"

When you're working with Flask, a popular Python web framework, you'll often need to run your application to see it in action. The command to do this is simple: `flask run`. But there's more to it than just that. Let's dive into the details of running a Flask app, including common options and best practices.

Understanding the `flask run` Command

The `flask run` command is the primary way to start your Flask application. It's a convenient wrapper around the `FLASK_APP` environment variable and the `flask` command-line tool. Here's a basic breakdown:

  • FLASK_APP is an environment variable that points to your Flask application's entry point. It's usually set to the name of your application's module, like myapp.
  • The flask command-line tool is a script that comes with Flask. It uses the FLASK_APP variable to find and run your application.

Running Your Flask App

To run your Flask app, simply open your terminal, navigate to your project's root directory (where your app.py or __init__.py file is located), and type:

Learn Flask [2026] Most Recommended Tutorials
Learn Flask [2026] Most Recommended Tutorials

flask run

This will start your application and display a URL where you can access it. By default, it will be something like http://127.0.0.1:5000/.

Using the `--host` and `--port` Options

You can customize the host and port used by your Flask app with the `--host` and `--port` options. For example, to run your app on localhost with a custom port of 8000, you would use:

flask run --host=localhost --port=8000

Using Environment Variables

You can also set the host and port using environment variables. This can be useful if you're running your app in a production environment where you might not have access to the command line. Here's how you can do it:

GitHub - pallets/flask: The Python micro framework for building web applications.
GitHub - pallets/flask: The Python micro framework for building web applications.

  • Set the FLASK_HOST environment variable to the host you want to use.
  • Set the FLASK_PORT environment variable to the port you want to use.

Then, simply run flask run as usual.

Running Your Flask App in Debug Mode

By default, Flask runs in debug mode when you use the `flask run` command. This means that it will automatically reload the server when you make changes to your code and provide a helpful debugger and debugger console. However, it's not recommended to run your app in debug mode in a production environment due to security risks.

Disabling Debug Mode

To disable debug mode, you can use the `--no-debugger` and `--no-reload` options:

Deploy Your Flask App Like a Pro: Nginx, Gunicorn, SSL & Custom Domain Setup
Deploy Your Flask App Like a Pro: Nginx, Gunicorn, SSL & Custom Domain Setup

flask run --no-debugger --no-reload

Alternatively, you can set the FLASK_ENV environment variable to production to disable debug mode:

export FLASK_ENV=production

Running Your Flask App with Gunicorn

While the `flask run` command is great for development, it's not recommended for production use. For production, you should use a WSGI server like Gunicorn. Here's how you can do it:

  • Install Gunicorn if you haven't already: pip install gunicorn
  • Run your app with Gunicorn: gunicorn app:app

This will start your app using Gunicorn, which is more suitable for production use.

Conclusion

Running a Flask app is a straightforward process, but there are many options and best practices to consider. Whether you're just starting out or you're a seasoned Flask developer, understanding the `flask run` command and its various options can help you work more efficiently and effectively.

What Can You Do With Flask? (Applications & Examples)
What Can You Do With Flask? (Applications & Examples)
How to spin up a simple Flask app using NGINX — in 15 minutes or less
How to spin up a simple Flask app using NGINX — in 15 minutes or less
Flask Web app
Flask Web app
a black and white poster with the words flask vs fastapp
a black and white poster with the words flask vs fastapp
Flask App - Film, Lurk & Ask - Erik Bernacchi
Flask App - Film, Lurk & Ask - Erik Bernacchi
Production Recipes: Flask App | Toptal®
Production Recipes: Flask App | Toptal®
an app icon with sound waves in the center and blue lines on top of it
an app icon with sound waves in the center and blue lines on top of it
Deploying Flask App on Netlify? Try This Smarter AI-Powered Alternative
Deploying Flask App on Netlify? Try This Smarter AI-Powered Alternative
| Pythonista Planet
| Pythonista Planet
How a Flask app works
How a Flask app works
Writing and Running a Flask Web App on the Raspberry Pi
Writing and Running a Flask Web App on the Raspberry Pi
Flask Boilerplate - Open-Source Apps built with automation tools
Flask Boilerplate - Open-Source Apps built with automation tools
Hike Flask №003 by Matthew Hall
Hike Flask №003 by Matthew Hall
a whale floating in the ocean next to a pile of shipping containers and a pipe
a whale floating in the ocean next to a pile of shipping containers and a pipe
GitHub - app-generator/flask-apps: Flask Apps - Open-Source And Paid | App-Generator
GitHub - app-generator/flask-apps: Flask Apps - Open-Source And Paid | App-Generator
an image of a water bottle with flames on it and other items to use in this project
an image of a water bottle with flames on it and other items to use in this project
GitHub - ramuta/example-azure-flask: How to deploy your Flask app to Azure (with example)
GitHub - ramuta/example-azure-flask: How to deploy your Flask app to Azure (with example)
the features of flask are shown in this graphic
the features of flask are shown in this graphic
a person holding a water bottle while riding a bike
a person holding a water bottle while riding a bike
an ad for tulink's debugging flask app with vs code id
an ad for tulink's debugging flask app with vs code id
Host a Serverless Flask App on Vercel
Host a Serverless Flask App on Vercel
Drink Piss Meme, Alcoholic Reaction Image, Alcohol Flask, Get In The Flask, Flask Aesthetic, Bottles Meme, Bottle Meme, Flask Aesthetic Alcohol, Alcohol Flask Aesthetic
Drink Piss Meme, Alcoholic Reaction Image, Alcohol Flask, Get In The Flask, Flask Aesthetic, Bottles Meme, Bottle Meme, Flask Aesthetic Alcohol, Alcohol Flask Aesthetic
Unixstickers's Store | Sticker Mule
Unixstickers's Store | Sticker Mule