Skip to main content

Flask Blueprint Architecture

Design a modular Flask application using Blueprints with factory pattern, extension management, configuration hierarchy, and testing setup.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Architect a Flask application for SaaS project management API using the application factory pattern with Blueprints. The app has 6 feature modules and will be deployed on Gunicorn behind Nginx on AWS ECS. Design: 1) Application factory (create_app) in app/__init__.py — accept config class name, initialize extensions (SQLAlchemy, Migrate, Login, CORS, Limiter), register blueprints with URL prefixes, configure logging to stdout in JSON format for container log aggregation, and set up error handlers for 400/404/422/500 returning JSON. 2) Blueprint structure for each module: auth, users, projects, tasks, notifications, billing — each blueprint in its own package with routes.py, models.py, schemas.py (Marshmallow or Pydantic), and services.py. 3) Configuration hierarchy: base Config, DevelopmentConfig, TestingConfig, ProductionConfig using environment variables loaded via python-dotenv with .env file. Handle DATABASE_URL, SECRET_KEY, STRIPE_API_KEY, SMTP credentials securely. 4) Database setup with Flask-SQLAlchemy — model base class with created_at/updated_at mixins, soft delete mixin, and UUID primary key mixin. 5) Authentication using JWT with Flask-JWT-Extended (access + refresh tokens) with role-based access control for roles: admin, manager, member, viewer. 6) Request validation and serialization using Marshmallow with Flask-Marshmallow with proper error message formatting. 7) A CLI command group (flask pm) for seed-db, create-admin, send-reminders, cleanup-expired-tokens. 8) Testing setup with pytest fixtures: test client, authenticated client, database with transactions rolled back per test.

What this prompt does

This prompt asks the AI to architect a complete Flask application around the application factory pattern and Blueprints. You describe the app with [app_purpose], tell it how many feature modules exist via [module_count], and name the [deployment_target], and it produces a create_app factory inside [package_name]/__init__.py that accepts a config class name, initializes extensions (SQLAlchemy, Migrate, Login, CORS, Limiter), registers blueprints with URL prefixes, wires logging to [log_destination], and adds JSON error handlers for 400/404/422/500.

The structure works because it forces separation of concerns up front. Each module in [blueprint_list] gets its own package with routes, models, schemas, and services, so features stay isolated as the app grows and the test suite stays honest. The config hierarchy keyed off [env_loader] lets the same code run safely in development, testing, and production, while [sensitive_configs] are handled through environment variables rather than hardcoding. Naming [auth_strategy] and [user_roles] shapes the role-based access-control layer, [serialization_lib] decides whether validation flows through Marshmallow or Pydantic, and the flask [cli_prefix] command group exposes operational tasks like [cli_commands].

When to use it

  • Starting a new Flask project you expect to grow past a single file
  • You need several feature areas (auth, billing, users) cleanly separated as Blueprints
  • You want one codebase to run identically across dev, test, and prod via config classes
  • You need a testing setup with per-test transaction rollback and authenticated client fixtures
  • You're standardizing model mixins like UUID primary keys and timestamp columns across a team
  • You want a flask [cli_prefix] command group for operational tasks like seeding or admin creation
  • You're handing the project to other engineers and want a structure they can navigate without a tour

Example output

The AI returns a layered project blueprint: a create_app factory accepting a config class name, a [package_name] directory tree with one sub-package per blueprint (routes.py, models.py, schemas.py, services.py), four config classes inheriting from a base Config, model mixins for timestamps and UUID keys, an auth module scoped to your roles, and a pytest fixture set with a test client, an authenticated client, and rolled-back transactions. Expect annotated code blocks for each numbered section rather than a single monolithic file, so you can adopt the pieces incrementally.

Pro tips

  • Set [module_count] and [blueprint_list] to match — if you list six blueprints, say six modules so the factory registration is consistent
  • Make [log_destination] explicit (for example, stdout in JSON) when targeting containers so log aggregation works without extra wiring
  • Use [sensitive_configs] to enumerate every secret (DATABASE_URL, SECRET_KEY, API keys) so none get baked into Config
  • Pin [auth_strategy] precisely — JWT with refresh tokens versus session auth changes the whole login module and the role checks for [user_roles]
  • Keep [cli_commands] focused on operational chores; bloating the CLI group with business logic makes it harder to test
  • Confirm the generated extension initialization matches what you actually install, since an unused extension in the factory will fail at startup
  • If the first pass feels thin on testing, re-prompt asking specifically for the pytest fixtures with rolled-back transactions per test

Frequently Asked Questions

Does this prompt use the application factory pattern or a single app instance?
It explicitly uses the application factory pattern, generating a `create_app` function in `[package_name]/__init__.py` that accepts a config class name. This is what lets you spin up separate app instances for testing and production from the same code.
Can I choose between Marshmallow and Pydantic for validation?
Yes. The `[serialization_lib]` variable controls the serialization and validation layer, and the prompt mentions Marshmallow or Pydantic as options. Set it to whichever your team prefers and the generated schemas.py files will follow that choice.
How does it keep secrets like the database URL out of the code?
It builds a configuration hierarchy that loads values through `[env_loader]` and handles `[sensitive_configs]` via environment variables. Secrets such as DATABASE_URL and SECRET_KEY are read from the environment rather than written directly into the config classes.
Will it include a testing setup, or just the app structure?
The prompt asks for pytest fixtures including a test client, an authenticated client, and a database that rolls back transactions per test. Testing is part of the requested output, though you should verify the generated fixtures match your actual extension setup before relying on them.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Django & Flask Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support