Skip to content

First Flask Application (Hello World)

This is the smallest useful Flask app.

python
from flask import Flask
 
app = Flask(__name__)
 
 
@app.route("/")
def home():
    return "Hello, Flask!"
  • app = Flask(__name__) creates the Flask application object.
    • __name__ helps Flask know where to find templates/static files later.
  • @app.route("/") registers a route.
    • When a request hits /, Flask calls home().
  • Returning a string becomes an HTTP response body.

Set the Flask app entry point:

bash
export FLASK_APP=app
flask run

You’ll see this pattern in many tutorials:

python
if __name__ == "__main__":
    app.run(debug=True)

This is valid, but for this tutorial track we’ll mainly use flask run because it matches production patterns better.

If you see “Working outside of application context” later, it’s usually because something that expects app is being used without a request/app context.

Don’t worry about it now—just remember Flask has context concepts.

Here’s how a single request flows through the WSGI server and your Flask app before a response comes back.

diagram WSGI request lifecycle mermaid
How a browser request travels through Flask and back

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading