liveBook · Manning

Routing

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A URL route is a web-framework mapping from a URL pattern or path to application code that handles requests for it.

Routing connects a URL path to the Python function that handles a request. In Flask, a route turns a small function into a browser-accessible endpoint, linking application code with HTTP requests. section 6.4.2

A practical routing workflow grows from one endpoint to a maintainable application:

  • Register a function with a route such as /. section 6.4.2
  • Return content from the function so the browser can render the response. section 6.4.2
  • Add functions and additional routes as the application needs more URLs. section 6.4.3
  • Keep substantial HTML in templates rather than embedding large HTML strings inside route functions. section 6.4.3

A first route

The decorator @app.route("/") registers home() for the root URL. When a browser visits the server at that path, Flask calls home() and returns its "Hello World" result. The server continues waiting for incoming HTTP requests through its event loop. section 6.4.2

from flask import Flask      #1
 
app = Flask(__name__)        #2
 
@app.route("/")              #3
def home():                  #4
    return "Hello World!"

Use the compact Flask listing to see the application instance, route decorator, handler function, and returned response together. section 6.4.2

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Defines decorator-based registration, request handling, and additional routes, while deferring broader application routing details.
Examples
Uses a Flask root route returning Hello World and a template-backed route rendering the current time.
What it assumes
Assumes basic Python functions and introduces Flask's application instance, route decorator, and request-serving behavior.
What you can do after
Build it from scratch by registering Flask routes, serving responses, adding endpoints, and passing dynamic data to templates.
How it connects
Connects decorators, HTTP requests, event loops, dynamic content, and templates.

Also explained in

Compare how 22 books explain routing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.