Search⌘ K
AI Features

Hello World!

Explore how to build your first Flask web application by setting up a minimal project structure, defining routes with decorators, and managing requests through a single Python script. Learn to run your app using the Flask command-line interface, preparing you to extend functionality in more complex applications.

We begin our exploration of back-end web service development by constructing a foundational application execution script. Flask operates as an extensible microframework, providing core routing and request handling utilities while leaving architectural layout choices entirely to our discretion. By starting with a single-file application, we establish a clean execution pattern that scales predictably into multi-module web architectures.

The minimal application structure

Web software development requires a predictable lifecycle where a client issues a request and a server returns a formatted response body. Before writing any code, we establish a clean, dedicated workspace directory to isolate our application files. For a minimal application, our workspace requires only a single Python source file.

We organize our project directory layout to establish an explicit home for our entry point script.

hello_world_app/
└── app.py
The structural directory layout of a minimal single-file Flask application workspace.

This single file contains all the processing logic required to respond to a web browser. The client browser communicates with our framework translation layer over HTTP, and Flask matches the incoming URL path to our custom code.

Before writing code inside our application file, let’s observe the core structural request-response workflow pipeline.

The request-response lifecycle illustrating the network interaction loop between a browser and a Flask application
The request-response lifecycle illustrating the network interaction loop between a browser and a Flask application

This network loop forms the structural core of all web applications. Once the request arrives at our Python server, the application must process it through a central coordinating registry container. This brings us directly to the initialization of the primary application framework element.

Defining the application instance

Every web service built with this framework depends on a central application object that coordinates routing paths, configuration parameters, and structural lifecycles. We instantiate this object from the native class library to register our application components. This registry serves as the core Web Server Gateway Interface application element, processing data translations between the upstream web server and our custom Python scripts.

To construct this interface, we must import the required module and initialize the application instance container.

# File: app.py
from flask import Flask
app = Flask(__name__)
Instantiation of the primary Flask application registry object
  • Line 2: Imports the foundational Flask class from the core framework package.

  • Line 4: Generates the central web application instance by passing the default execution name variable to the class constructor.

The use of the __name__ argument allows the underlying object to determine absolute folder paths for static assets and HTML layout components on the host storage disk. With this central instance established, we can attach accessible network entry paths to our backend execution logic.

Routing with decorators

Web servers direct incoming requests to specific Python functions by evaluating the URL path provided by the web browser. Flask handles this matching mechanism through decorators, which are syntax structures that modify or extend Python function behaviors. By prefixing a standard function with a route decorator, we explicitly instruct the central application instance to execute that function whenever a matching URL path is requested.

We examine the complete program implementation containing the imported module, the initialized instance, and the decorated view function.

A modern, minimal Flask application containing an explicit root URL path mapping
  • Line 1: Imports the central framework class needed to bootstrap our web server.

  • Line 3: Creates the core application registry container under the variable name app.

  • Line 5: Applies the routing decorator to bind the root web path directly to the underlying execution function.

  • Line 6: Declares the view function that will process the incoming web request for the mapped path.

  • Line 7: Passes a plain text string back to the framework execution engine to build an HTTP response payload.

The function we defined returns a raw text string that the framework automatically converts into a valid HTTP response with a successful status code. To understand the hierarchical routing layout of our script, we can organize these software components into an ordered tree.

{
"root": {
"Application Instance (app)": {
"URL Route Mapping (/)": {
"View Function (hello)": {
"Output Payload": "Hello World!"
}
}
}
}
}
Hierarchical organization of modern Flask framework routing bindings

Our structural components are now fully registered within the application script file. To run this application code and view the string response in a live browser window, we transition to the modern framework execution terminal management tools.

Executing via the Flask CLI

We execute modern applications using the official command-line interface tool rather than invoking programmatic run methods directly within our code files. The CLI centralizes runtime control, enabling us to toggle local development debug safeguards and inject environment variables directly from the shell terminal. This approach keeps our Python source files free of hardcoded execution configurations like ports and network addresses.

We launch our web script file from the system shell terminal by providing the file location flag alongside debug configuration parameters.

flask --app app run --host 0.0.0.0 --port 3000 --debug
Launching the local web server using the modern framework command-line interface flags
  • flask: Executes the framework binary utility script in the system shell.

  • --app app: Identifies the target Python module filename, app.py, containing our initialized application instance.

  • run: Instantiates the local development network server loop.

  • --host 0.0.0.0: Configures the server to listen on all available network interfaces.

  • --port 3000: Explicitly sets the network port to 3000.

  • --debug: Activates development utilities, including the automatic filesystem reloader and the interactive error browser.

Employing the CLI workflow cleanly separates configuration management from Python application execution, establishing decoupled environment habits early. This server loop runs continuously in our terminal to listen for inbound browser requests.

This command pattern represents the standard execution workflow for local framework development. Every advanced service path and network route we construct throughout this course will build directly upon this modern command utility structure.

We have successfully built, mapped, and launched our first microframework web server application. By relying on explicit route decorators and modern command-line interface utilities, we have separated our source code definitions from external runtime environments. This clean demarcation ensures our code remains transportable and ready to integrate sophisticated presentation templates, input validation components, and relational storage engines in the upcoming lessons.