- 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.- Importing
routesinsideapp.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 usescurrent_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.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. Usepython run.py or set FLASK_APP and use flask run.
Running the application
- Activate your virtual environment.
- Start the app. You have two common options:
-
Using Flask CLI (set
FLASK_APPthen runflask run) macOS / Linux:Windows (cmd): -
Or run directly:
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). ConfirmFLASK_APPor run viapython run.py. - Your app factory does not import the
routesmodule 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.
/routes is not listing registered routes and every request returns 404.
Fix checklist:
- Import routes inside
create_app()(insidewith app.app_context(): from . import routes) so the module-level@app.routedecorators register against the created app. - Avoid creating another
Flask(__name__)inroutes.py. Usecurrent_appor, preferably, Blueprints that you register from the factory.
/ and /routes.

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.
Links and references
- Flask documentation: https://flask.palletsprojects.com/
- Flask application factory pattern: https://flask.palletsprojects.com/en/latest/patterns/appfactories/
- Flask Blueprints: https://flask.palletsprojects.com/en/latest/blueprints/