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

How to Run a Flask Application

Last Updated : 21 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The backend server Flask was created fully in Python. It is a framework made up of Python modules and packages. With its characteristics, it is a lightweight Flask application that speeds up the development of backend apps. We will learn how to execute a Flask application in this tutorial.

Run Flask application Syntax

We can run the Flask application using the below command.

flask –app <hello> run

flask run

python <app_name>.py

File Structure

Here, we are using the following folder and file.

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

Run a Flask Application

In this example, we have an application called helloworld.py below is the basic code for Flask. 

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()  

Output:

Using flask –app <app_name> run

Screenshot-from-2023-04-13-18-04-17

Using flask run

Screenshot-from-2023-04-13-17-59-57

Using the python app_name.py

Capture14

3-300x64

Run the app in the debugger

We will use the below command to run the flask application with debug mode as on. When debug mode is turned on, It allows developers to locate any possible error and as well the location of the error, by logging a traceback of the error.

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

Capture15

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