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 callshome()home().
- When a request hits
- Returning a string becomes an HTTP response body.
Run it (two common ways)
Option A: flask runflask run (recommended)
Set the Flask app entry point:
bash
export FLASK_APP=app
flask runbash
export FLASK_APP=app
flask runOption 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.
flowchart LR B["Browser"] --> W["WSGI server"] W --> F["Flask app"] F --> M["Matched view function"] M --> R["Response"] R --> B
🧪 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 coffeeWas this page helpful?
Let us know how we did
