Skip to main content
This guide shows a minimal, debuggable Flask API using the application factory pattern. It covers the working file layout, the key files (factory, routes, template, run script), a simple debugging workflow for 404s caused by unregistered routes, and next steps for extending the API. Highlights
  • Application factory pattern to create and configure the Flask app.
  • Register routes inside the app context to ensure decorators bind to the created app.
  • Troubleshooting steps when every request returns 404.

Project structure (relevant files)

A short description of the files used in this example and their purpose.

app/init.py

Use an application factory. Keep configuration loading minimal to start, and import (register) your routes while an application context is active so route decorators register on the created app instance.
Notes:
  • Importing routes inside app.app_context() ensures route decorators register on the created app.
  • For larger projects prefer Blueprints (recommended). Importing routes inside the context is an acceptable pattern for small apps and demos.

app/routes.py

A minimal set of routes plus a helper endpoint to list registered routes. This example uses current_app (aliased as app) so the @app.route decorators attach to the application instance that created the context.
Using current_app (imported as app) and applying @app.route at module import time is fragile: it only works because create_app() imports the routes module inside an application context. For maintainability and to avoid import-time issues as your application grows, prefer defining routes in a Blueprint and registering that Blueprint from the factory.
Alternative (recommended for larger projects): define a Blueprint in this module and register it in create_app() from app/__init__.py. For this minimal app, importing routes in the app context is sufficient and simpler.

app/templates/base.html

Minimal template rendered by the / route. It includes a small block so the template can be extended later.

run.py

Entrypoint that creates the app with the factory and runs it in development mode. Use python run.py or set FLASK_APP and use flask run.

Running the application

  1. Activate your virtual environment.
  1. Start the app. You have two common options:
  • Using Flask CLI (set FLASK_APP then run flask run) macOS / Linux:
    Windows (cmd):
  • Or run directly:
When Flask starts successfully you should see the factory’s print statement and server output similar to:

Troubleshooting: 404 for all routes

If Flask starts but every request returns 404 (including /routes), check the following common causes:
  • FLASK_APP is not pointing to the correct entrypoint (e.g. run.py). Confirm FLASK_APP or run via python run.py.
  • Your app factory does not import the routes module while an application context is active, so decorators never run.
  • You accidentally create a second Flask app instance in routes.py (instead of using the created app) and apply decorators to that unrelated instance.
Typical symptom: server logs show the app started but /routes is not listing registered routes and every request returns 404. Fix checklist:
  • Import routes inside create_app() (inside with app.app_context(): from . import routes) so the module-level @app.route decorators register against the created app.
  • Avoid creating another Flask(__name__) in routes.py. Use current_app or, preferably, Blueprints that you register from the factory.
After applying these changes and restarting, you should see “Routes imported successfully” and get 200 responses for / and /routes.
A Chrome browser screenshot showing a "Not Found" (404) page. The page is served from localhost (127.0.0.1:5000/routes) with a message that the requested URL was not found on the server.

If you plan to extend this API, switch to Blueprints for route registration. Blueprints decouple route definitions from application creation and avoid import-time issues as your project grows. See the Flask docs for Blueprints and application factories for recommended patterns.

Next steps

  • Add JSON API endpoints for image upload and processing.
  • Implement validation and security for file uploads (size limits, allowed types).
  • If you serve a separate React frontend, keep Flask as a JSON API and enable CORS or use a reverse proxy (Nginx) for production deployments.
  • Replace the development server with a production WSGI server (e.g., Gunicorn or uWSGI) when deploying.

Watch Video