Skip to content

Flask - Application

Let’s create a minimal Flask application to verify your setup and understand the basic structure.

Create a file named hello.py (or any other .py name) and add the following code:

# 1. Import the Flask class
from flask import Flask
# 2. Create an instance of the Flask class
# __name__ tells Flask where to look for resources like templates and static files.
app = Flask(__name__)
# 3. Define a route using the route() decorator
# This binds the URL '/' to the hello_world function.
@app.route('/')
def hello_world():
# 4. This function is the view function.
# It returns the response that will be sent to the browser.
return 'Hello, World!'
# 5. Check if the script is executed directly (not imported)
if __name__ == '__main__':
# 6. Run the application using the built-in development server.
# debug=True enables debug mode (explained below).
app.run(debug=True)
  1. Import Flask: We start by importing the necessary class from the flask package.
  2. Create app Instance: We create an instance of the Flask class. This instance is our WSGI (Web Server Gateway Interface) application. The __name__ argument is a standard Python variable that holds the name of the current module. Flask uses this to determine the application’s root path, which helps locate related resources like templates and static files.
  3. Define a Route (@app.route): The @app.route('/') decorator registers the following function (hello_world) to handle requests for the root URL (/).
  4. View Function: The hello_world() function is called when a request comes in for the / URL. It simply returns the string ‘Hello, World!’. This string will be sent back to the client (e.g., web browser) as the HTTP response body.
  5. Run Check (if __name__ == '__main__':): This standard Python construct ensures that the code inside this block only runs when the script is executed directly (e.g., by running python hello.py). It prevents the server from starting if the script is imported into another module.
  6. Start the Server (app.run()): This method starts Flask’s built-in development web server. It listens for incoming requests and routes them to the appropriate view functions.
  1. Make sure your virtual environment is activated.

  2. Open your terminal, navigate to the directory where you saved hello.py.

  3. Run the script using the Python interpreter:

python hello.py

You should see output similar to this (the exact address and port might vary slightly):

* Serving Flask app 'hello' (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: ...

This indicates that the development server is running.

  1. Open your web browser and go to the URL shown: http://127.0.0.1:5000/ (or http://localhost:5000/).

You should see the text ‘Hello, World!’ displayed on the page.

We started the server using app.run(debug=True). Enabling debug mode provides several benefits during development:

  • Automatic Reloader: The server automatically restarts whenever you save changes to your code. This saves you from manually stopping and starting the server after every modification.
  • Interactive Debugger: If an error occurs during a request, Flask displays a detailed traceback in the browser, allowing you to inspect the code, variables, and execution stack at the point of failure. This is extremely helpful for diagnosing problems.
  • Security Warning: Never run a Flask application with debug=True in a production environment! The interactive debugger allows arbitrary code execution, which poses a major security risk.

The app.run() method accepts several optional parameters:

ParameterDescription
hostThe hostname to listen on. Defaults to '127.0.0.1' (localhost), meaning the server is only accessible from your own computer. Set to '0.0.0.0' to make the server accessible from other devices on your network (use with caution).
portThe port number to listen on. Defaults to 5000.
debugSet to True to enable debug mode. Defaults to False if the FLASK_ENV environment variable is not set to development.
optionsAdditional options to be forwarded to the underlying Werkzeug server.

For example, to run on port 8080 and make it accessible on your network:

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)