Note 14.5.1.
If you are using a Mac or Linux computer, use the following command to install flask:
pip3 install flask
and execute your flaskhello.py program using the following command:
python3 flaskhello.py
from flask import Flask
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def hello():
return """<html><body>
<h1>Hello, world!</h1>
The time is """ + str(datetime.now()) + """.
</body></html>"""
if __name__ == "__main__":
# Launch the Flask dev server
app.run(host="localhost", debug=True)
hello()
that serves up a simple web page containing the date and time. The call to app.run()
on Line 14 starts a small web server. The run()
method does not return; it executes an infinite loop that waits for incoming requests from web browsers. When a web browser sends a request to the Flask web server, the server invokes the hello()
function and returns the HTML code generated by the function to the web browser, which displays the result.pip install flask
python flaskhello.py
pip3 install flask
python3 flaskhello.py
* Serving Flask app "sample" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 244-727-575 * Running on http://localhost:5000/ (Press CTRL+C to quit)
http://localhost:5000/1
http://localhost:5000/