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
pipis 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
.envand.flaskenvfiles ifpython-dotenvis installed. - Instance Folders: Use
instance_relative_config=Truewhen 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 usingasgiref.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
flaskcommand is not found, ensure your virtual environment is activated or that the script path is in yourPATH. - Import Errors: Ensure you have installed the package in editable mode (
pip install -e .) or that yourPYTHONPATHincludes thesrcdirectory.