Skip to content

First Flask Application (Hello World)

This is the smallest useful Flask app.

Create app.pyapp.py

from flask import Flask
 
app = Flask(__name__)
 
 
@app.route("/")
def home():
    return "Hello, Flask!"
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:

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

Option B: python app.pypython app.py

You’ll see this pattern in many tutorials:

if __name__ == "__main__":
    app.run(debug=True)
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.

πŸ§ͺ 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