Skip to content

First Flask Application (Hello World)

This is the smallest useful Flask app.

Create app.pyapp.py

python
from flask import Flask
 
app = Flask(__name__)
 
 
@app.route("/")
def home():
    return "Hello, Flask!"
python
from flask import Flask
 
app = Flask(__name__)
 
 
@app.route("/")
def home():
    return "Hello, Flask!"

What’s happening?

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

Run it (two common ways)

Set the Flask app entry point:

bash
export FLASK_APP=app
flask run
bash
export FLASK_APP=app
flask run

Option B: python app.pypython app.py

You’ll see this pattern in many tutorials:

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

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

Common beginner error

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

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

Visualize it

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

🧪 Try It Yourself

Exercise 1 – Create a Flask App

Exercise 2 – Dynamic Route

Exercise 3 – Return JSON

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did