Hello World
The classic starting point for any framework. A complete Flask application can be as short as five lines:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
app.run()
The magic here is the @app.route("/") decorator. It tells Flask that whenever a user visits the root URL (/), the hello() function should handle the request. Whatever that function returns becomes the response sent back to the browser.
You're not limited to strings โ but we'll cover more response types later. For now, let's focus on getting the server running.
When you call app.run(), Flask starts a built-in development server. It listens on 127.0.0.1:5000 by default. You can change the host and port:
app.run(host="0.0.0.0", port=8080)
Setting the host to 0.0.0.0 makes the server accessible from other devices on your network โ useful when testing on a phone or sharing with a colleague.
Enable debug mode and you get automatic reloading whenever you change your code. No more manually restarting the server after every edit:
app.run(debug=True)
The development server is great for local work, but don't use it in production. For that, you'll want something like Gunicorn or uWSGI behind a reverse proxy like Nginx.