Serving HTML pages
First, to make our hello
function respond with HTML, all we have to do is change it like this:
def hello(): return "<html><head><title>Hi there!</title></head><body>Hello World!</body></html>", 200
In the preceding example, hello
is returning a HTML formatted string and a number. The string will be parsed as HTML by default while 200
is an optional HTTP code indicating a successful response. 200
is returned by default.
If you refresh your browser with F5, you'll notice that nothing has changed. That's why the Flask development server is not reloading when the source changes. That only happens when you run your application in debug mode. So let's do that:
app = Flask(__name__) app.debug=True
Now go to the terminal where your application is running, type Ctrl + C
then restart the server. You will notice a new output besides the URL where your server is running—something about "stat"...