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











