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.
  • Tools: This project uses uv for dependency management, but pip is also supported.

Installation

To install Flask and its dependencies for development, use the following commands:

# Clone the repository
git clone https://github.com/pallets/flask/
cd flask

# Install dependencies using uv
uv sync

Alternatively, you can install it using pip:

pip install .

Hello World / Quick Start

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

from flask import Flask

# Create an instance of the Flask class
app = Flask(__name__)

# Define a route for the root URL
@app.route("/")
def hello():
return "Hello, World!"

Run the Server

You can run your application using the flask CLI:

flask --app app run

The server will start running on http://127.0.0.1:5000/.

Configuration

Flask applications can be configured through the app.config object. Common configuration patterns include:

  • Environment Variables: Flask can automatically load variables from .env and .flaskenv files if python-dotenv is installed.
  • Instance Folders: Use instance_relative_config=True when creating the app to load configuration files from an instance folder that is not committed to version control.

Example of setting a secret key:

app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY="dev",
)

Verify Installation

To ensure everything is set up correctly, run the test suite:

pytest

Other Install Options

Optional Dependencies

Flask provides several extras for additional functionality:

  • async: Adds support for async routes using asgiref.
  • dotenv: Adds support for loading environment variables from files.

Install them using:

pip install "Flask[async,dotenv]"

Next Steps

  • Tutorial: Follow the tutorial to build a complete blog application.
  • Blueprints: Learn how to organize your application into components using Introduction to Blueprints.
  • CLI: Explore more commands available in the Command Line Interface.
  • Deployment: Read about deployment options for production environments.

Troubleshooting

  • Command Not Found: If the flask command is not found, ensure your virtual environment is activated or that the script path is in your PATH.
  • Import Errors: Ensure you have installed the package in editable mode (pip install -e .) or that your PYTHONPATH includes the src directory.