NelsonLabs
Flask/Setup & First App

Setup & First App

A minimal Flask app is 5 lines of Python. That simplicity is intentional — Flask gets out of your way so you can focus on what makes your application unique.

Setting up a Flask project
bash
# Create virtual environment
python -m venv venv
source venv/bin/activate    # Mac/Linux
venvScriptsactivate        # Windows

# Install Flask
pip install flask

# app.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
    return "Hello from Flask!"

if __name__ == "__main__":
    app.run(debug=True)
Running Flask
bash
# Option 1: run directly
python app.py

# Option 2: using Flask CLI (recommended)
export FLASK_APP=app.py        # Mac/Linux
set FLASK_APP=app.py           # Windows
export FLASK_DEBUG=1           # enables auto-reload
flask run

# Visit http://127.0.0.1:5000/

TIP

Use an app factory pattern for larger projects. For anything beyond a single file, use an application factory function (create_app()). This makes your app testable, configurable, and ready for Blueprints. We'll cover this in the Blueprints chapter.