The Wayback Machine - https://web.archive.org/web/20250329174943/https://www.geeksforgeeks.org/how-to-run-a-flask-application/
Open In App

How to Run a Flask Application

Last Updated : 19 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

After successfully creating a Flask app, we can run it on the development server using the Flask CLI or by running the Python script.

Simply execute one of the following commands in the terminal:

  • flask –app app_name run
  • python app_name

File Structure

Here, we are using the following folder and file.

Screenshot-from-2023-04-13-18-07-25

Demonstration with an Example

In this example, we have a basic application called helloworld.py, below is the code for it and we will run this app with the debugger mode on. 

Python
# import flast module
from flask import Flask

# instance of flask application
app = Flask(__name__)

# home route that returns below text when root url is accessed
@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__ == '__main__':  
   app.run()  

Now let’s run the app using both the commands seperately.

Using flask –app <app_name> run

flask--app

flask –app helloworld.py run

Using the python app_name.py

pythonapp

python helloworld.py

Below is a snippet of the live app running on the development server. You can open it by Ctrl + Click on the link: “http://127.0.0.1:5000” or by typing the URL directly into your browser.

runpreview

http://127.0.0.1:5000

Run the app in the debugger

With debug mode on, Flask automatically detects errors and shows a detailed traceback, helping developers quickly find and fix issues. We will use the below command to run the flask application with debug mode as on.

if __name__ == ‘__main__’:  
  app.run(debug = True)

debugon

Running app with debug=True

How to Run a Flask Application – FAQs

How Do I Run a Production Flask App?

Running a Flask application in production involves setting up a more robust and secure server than the built-in development server provided by Flask. Here are the steps to deploy a Flask app for production:

  1. Choose a WSGI Server: For production, it’s recommended to use a WSGI server like Gunicorn or uWSGI. These servers are designed to manage and handle requests in a production environment efficiently.Example with Gunicorn (on Linux/Mac):
    • -w 4 specifies four worker processes. Replace app:app with your Flask app’s module and variable names.
  2. Use a Reverse Proxy: Setting up a reverse proxy like Nginx or Apache in front of your WSGI server can enhance security and performance. The reverse proxy handles HTTP requests, serving static files, and directing other requests to the WSGI server.
  3. Configuration and Security: Configure your server for security, such as setting up HTTPS using SSL/TLS, configuring firewalls, and ensuring all dependencies are up to date.
  4. Environment Variables: Use environment variables to manage configuration settings and sensitive information, keeping them out of the codebase.
pip install gunicorn
gunicorn -w 4 app:app

How Do I Run Flask App in Windows Terminal?

To run a Flask application in the Windows Terminal:

  1. Set Environment Variables:
  2. Run the Flask Application:
    • This command will start the Flask development server. Ensure your Flask script is named app.py, or change FLASK_APP to your script’s name.
set FLASK_APP=app.py
set FLASK_ENV=development
flask run

How Do I Make My Flask App Executable?

To make your Flask app executable, meaning creating a standalone application that can be run directly without explicitly running Python, you can use tools like PyInstaller:

  1. Install PyInstaller:
  2. Create an Executable:
    • --onefile tells PyInstaller to bundle your app and all its dependencies into a single executable. Replace app.py with the path to your Flask script.
  3. Run the Executable: After PyInstaller completes, find your executable in the dist folder. You can run it directly, and it will start your Flask application.
pip install pyinstaller
pyinstaller --onefile app.py

How Do I Run Flask REST API?

Running a Flask REST API is similar to running any Flask app but tailored towards handling API requests:

  1. Define Your Flask App:
  2. Run Your Flask App: In your command line (e.g., Windows Terminal), set the environment variables and start the server.
    • This starts your Flask REST API which can now respond to HTTP requests at the defined routes.
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api/data', methods=['GET'])
def get_data():
    data = {'key': 'value'}
    return jsonify(data)

if __name__ == '__main__':
    app.run(debug=True)
set FLASK_APP=app.py
set FLASK_ENV=development
flask run

These steps will help you manage both development and production setups for Flask applications, making your web applications more robust, secure, and efficient.



Next Article

Similar Reads

three90RightbarBannerImg