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: uv is the recommended tool for installation and dependency management, though
pipis also supported.
Installation
Install Flask using uv:
uv pip install Flask
Optional Dependencies
Flask supports several optional features that can be installed as extras:
- Async support: For using
asyncroutes and handlers. - Dotenv support: For automatically loading environment variables from
.envand.flaskenvfiles.
uv pip install "Flask[async,dotenv]"
Hello World / Quick Start
Create a file named app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
Run the Application
Use the flask CLI to start the development server:
flask run
The application will be available at http://127.0.0.1:5000/.
Application Factory Pattern
For larger applications, it is recommended to use the The Flask Application Object pattern. Create a directory for your package and an __init__.py file:
# myapp/__init__.py
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
@app.route("/hello")
def hello():
return "Hello, World!"
return app
Run it by specifying the factory:
flask --app myapp:create_app run
Configuration
Flask can be configured via environment variables or configuration files.
FLASK_APP: Specifies the application to load.FLASK_DEBUG: Set to1ortrueto enable debug mode (includes interactive debugger and reloader).
Example using environment variables:
export FLASK_APP=app.py
export FLASK_DEBUG=1
flask run
If python-dotenv is installed, Flask will automatically load variables from .env or .flaskenv in the project root.
Verify Installation
Confirm Flask is installed correctly by checking the version:
flask --version
To run the project's test suite (if you have cloned the repository):
pytest
Other Install Options
Using pip
If you prefer standard pip:
python -m venv .venv
source .venv/bin/activate # Linux/macOS
# or .venv\Scripts\activate on Windows
pip install Flask
Next Steps
- Routing: Learn how to handle different URLs and HTTP methods using Routing and View Function Registration.
- Templates: Use Understanding the Flask Jinja Environment to render HTML templates.
- Blueprints: Organize your application into components with Introduction to Blueprints.
- Testing: Write unit tests for your application using the Testing Flask Applications.