liveBook · Manning

HTML

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

A web page is a browser-rendered presentation of web content, commonly structured by an HTML document.

A browser can display a greeting returned by a web server, but a useful page needs more than a short string. A server commonly responds to an HTTP request with an HTML document, which the browser interprets and renders as a web page. section 6.3

HTML supplies the page content. Links in the document can prompt the browser to request CSS for styling and JavaScript for browser-side actions and updates. Rendering one page can therefore involve several requests, not just the initial HTML response. section 6.3

A Flask route can return a string containing HTML, but keeping hundreds of lines of markup inside Python functions makes pages difficult to maintain. Put the HTML in a separate template instead. The route can supply changing data—such as a calculation or database result—when it renders that template. section 6.4.3

A page showing the current time

A Flask route calls render_template("index.html", now=datetime.now()). The HTML template displays that supplied value with <h1>Current time: {{now}}</h1>. The page stays in templates/index.html while the Python route provides its data. section 6.4.3

from flask import Flask, render_template                       #1
from datetime import datetime                                  #2
 
app = Flask(__name__)
 
@app.route("/")
def home():
    return render_template("index.html", now=datetime.now())   #3

The route supplies a current value when it renders the HTML template. section 6.4.3

<!DOCTYPE html>                                                        #1
<html>                                                                 #1
 
<head>                                                                 #1
    <!-- Required meta tags -->                                        #1
    <meta charset="utf-8">                                             #1
    <meta                                                              #1
    name="viewport"                                                  #1
    content="width=device-width, initial-scale=1, shrink-to-fit=no"  #1
                                                                     #1
    <title>Your First Web Server</title>                               #1
</head>                                                                #1
 
<body>
    <h1>Current time: {{now}}</h1>                                     #2
</body>
 
</html>

The HTML document contains the heading and the placeholder that receives the route’s value. section 6.4.3

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

How deep it goes
Works through browser rendering and a Flask template example without detailing HTML syntax or HTTP protocol rules.
Examples
Uses a Flask route and an HTML template with a current-time placeholder.
What it assumes
Assumes a running Python Flask route and moves from its string response to a separate HTML template.
What you can do after
Apply it via Flask and Jinja2 to serve an HTML page whose displayed value comes from Python.
How it connects
Connects HTML to HTTP request-response, CSS, JavaScript, Flask routes, and Jinja2 templates.

Also explained in

Compare how 12 books explain HTML →

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.