Browse Courses

Routes

This document explains how to create and configure routes in Flask, return responses, manage application configuration, and structure Flask projects for maintainability. It covers decorators, JSON responses, environment variables and best practices for organizing code.

This document details how to create and configure routes in Flask, return responses, manage configuration, and structure projects for maintainability. It covers decorators, JSON responses, environment variables, and best practices for organizing Flask code.


Introduction to Flask Routes

Routes in Flask define how URLs are handled by the application. Each route is associated with a function that returns a response to the client.


Creating a Basic Flask Application

  1. Install Flask and create a Python file (e.g., app.py).
  2. Import the Flask class and instantiate the app:
1from flask import Flask
2app = Flask(__name__)
  1. Define a route using the @app.route decorator:
1@app.route('/')
2def hello_world():
3    return '<b>my first Flask application in action!</b>'

Running the Flask Application

  • Set environment variables for the app and environment:
1export FLASK_APP=app.py
2export FLASK_ENV=development
3flask run
  • The app runs on http://localhost:5000 by default.
  • Use --app and --debug flags for configuration and development mode.

Returning Responses and JSON

  • Return text or HTML directly from route functions.
  • To return JSON, return a serializable object (e.g., dictionary) or use jsonify:
1from flask import jsonify
2@app.route('/json')
3def json_example():
4    return jsonify(message='Hello, JSON!')
  • The response will have content type application/json.

Flask Configuration Options

  • Set configuration via environment variables, the app.config object, or external files.

  • Common options:

    • ENV: Environment type (development/production)
    • DEBUG: Enables debug mode
    • TESTING: Enables testing mode
    • SECRET_KEY: Signs session cookies
    • SESSION_COOKIE_NAME: Name of session cookie
    • SERVER_NAME: Host and port binding
    • JSONIFY: Defaults to application/json
  • Load configuration from a file using app.config.from_file().


Project Structure Best Practices

  • Organize code into module directories
  • Store configuration in separate files
  • Place static assets (images, JS, CSS) in a static directory
  • Use a templates directory for dynamic content
  • Keep test files in a dedicated test directory
  • Use a virtual environment for dependencies

Example structure:

1myapp/
2  app.py
3  config.py
4  static/
5  templates/
6  tests/
7  venv/

Conclusion

Flask routes map URLs to functions, enabling flexible response handling. Proper configuration and project structure improve maintainability and scalability of Flask applications.


FAQ

  1. To define how URLs are handled by the application
  2. To manage database connections
  3. To install Python packages
  4. To create virtual environments
(1) Routes map URLs to functions that return responses to the client.

  1. Enables debug mode and automatic restarts
  2. Disables all routes
  3. Changes the default port
  4. Prevents JSON responses
(1) Setting FLASK_ENV to ‘development’ enables debug mode and auto-reload.

  1. Returning a dictionary from a route
  2. Using the jsonify method
  3. Returning a string with HTML tags
  4. Returning a list from a route
(3) Returning a string with HTML tags does not produce a JSON response.

  1. Through environment variables
  2. By setting values in the app.config object
  3. By loading from external files
  4. All of the above
(4) Flask supports configuration via environment variables, app.config, and external files.

OptionPurpose
A. DEBUG1. Signs the session cookie
B. SECRET_KEY2. Enables debug mode
C. SERVER_NAME3. Binds the host and port
A-2, B-1, C-3.

  1. Organizing code and assets improves maintainability
  2. All code should be in a single file
  3. Static and dynamic content should be mixed
  4. Tests are not needed for Flask apps
(1) A well-structured project is easier to maintain and scale.

The @app.route decorator in Flask is used to map a URL to a function that returns a response.

True. The @app.route decorator defines the URL pattern and associates it with a function.