Skip to main content

Getting Started

Flask is a lightweight WSGI web application framework designed to make getting started quick and easy, with the ability to scale up to complex applications.

Prerequisites

  • Python: Version 3.10 or newer is required.
  • Package Manager: This guide assumes you are using uv, but you can also use pip.

Installation

The recommended way to install Flask is using uv. This will install Flask and its core dependencies: Werkzeug (WSGI), The Flask Jinja Environment (templating), Getting Started with the Flask CLI (CLI), and others.

uv add flask

Optional Dependencies

Flask supports several optional features that can be installed as extras:

  • Async: Adds support for async routes and handlers.
    uv add flask[async]
  • Dotenv: Automatically loads environment variables from .env and .flaskenv files.
    uv add flask[dotenv]

Hello World / Quick Start

Create a file named app.py with the following content:

from flask import Flask

# Create the application instance
app = Flask(__name__)

# Define a route and its handler
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

Run the Server

Use the flask CLI to start the development server:

flask run

The application will be available at http://127.0.0.1:5000/.

Configuration

Flask can be configured via environment variables or by loading configuration files.

Environment Variables

  • FLASK_APP: Specifies the application to load. If not set, Flask looks for app.py or wsgi.py.
  • FLASK_DEBUG: Set to 1 to enable debug mode, which provides an interactive debugger and reloads the server on code changes.
export FLASK_APP=hello
export FLASK_DEBUG=1
flask run

Instance Folders

For configuration that should not be committed to version control (like secret keys), Flask supports an instance folder.

app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile("config.py", silent=True)

Verify Installation

Confirm that Flask is installed correctly by checking its version:

flask --version

This will output the versions of Python, Flask, and Werkzeug.

Next Steps

Troubleshooting

  • Command not found: If the flask command is not found, ensure your virtual environment is activated or use python -m flask.
  • Address already in use: If port 5000 is occupied, you can specify a different port: flask run --port 8080.
  • ImportError: If you encounter an ImportError when running flask run, ensure FLASK_APP is set correctly to the module containing your Flask instance.