LLM Skills
~/catalog/backend//app-factory
BackendGitHub source

The "application factory" pattern in Flask

/app-factory

Build a scalable Flask application using the "factory" pattern with blueprints and configuration management.

davila7davila7
30.6k
May 22, 2026
MIT License
// skill content

Flask Application Factory Pattern Create a scalable Flask application using the factory pattern with blueprints and configuration management. ## Purpose This command helps you set up a Flask application using the application factory pattern, which is the recommended approach for larger Flask applications. ## Usage `` /app-factory ` ## What this command does 1. **Creates application factory** with proper structure 2. **Sets up configuration management** for different environments 3. **Implements blueprints** for modular design 4. **Configures extensions** (database, auth, etc.) 5. **Adds error handling** and logging ## Example Output `python # app/__init__.py from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager from flask_mail import Mail from flask_wtf.csrf import CSRFProtect from flask_cors import CORS import logging from logging.handlers import RotatingFileHandler import os # Initialize extensions db = SQLAlchemy() migrate = Migrate() login = LoginManager() mail = Mail() csrf = CSRFProtect() cors = CORS() def create_app(config_class=None): """Application factory function.""" app = Flask(__name__) # Load configuration if config_class is None: config_class = os.environ.get('FLASK_CONFIG', 'development') if isinstance(config_class, str): from app.config import config app.config.from_object(config[config_class]) else: app.config.from_object(config_class) # Initialize extensions db.init_app(app) migrate.init_app(app, db) login.init_app(app) mail.init_app(app) csrf.init_app(app) cors.init_app(app) # Configure login manager login.login_view = 'auth.login' login.login_message = 'Please log in to access this page.' login.login_message_category = 'info' # Register blueprints from app.main import bp as main_bp app.register_blueprint(main_bp) from app.auth import bp as auth_bp app.register_blueprint(auth_bp, url_prefix='/auth') from app.api import bp as api_bp app.register_blueprint(api_bp, url_prefix='/api') from app.admin import bp as admin_bp app.register_blueprint(admin_bp, url_prefix='/admin') # Error handlers from app.errors import bp as errors_bp app.register_blueprint(errors_bp) # Configure logging if not app.debug and not app.testing: if not os.path.exists('logs'): os.mkdir('logs') file_handler = RotatingFileHandler( 'logs/app.log', maxBytes=10240, backupCount=10 ) file_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]' )) file_handler.setLevel(logging.INFO) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) app.logger.info('Flask application startup') return app # Import models (avoid circular imports) from app import models ` ``python # app/config.py import os from dotenv import loaddotenv basedir = os.path.abspath(os.path.dirname(file)) loaddotenv(os.path.join(basedir, '.env')) class Config: """Base configuration class.""" SECRETKEY = os.environ.get('SECRETKEY') or 'dev-secret-key' SQLALCHEMYDATABASEURI = os.environ.get('DATABASEURL') or \ 'sqlite:///' + os.path.join(basedir, 'app.db') SQLALCHEMYTRACKMODIFICATIONS = False # Mail configuration MAILSERVER = os.environ.get('MAILSERVER') MAILPORT = int(os.environ.get('MAILPORT') or 587) MAILUSETLS = os.environ.get('MAILUSETLS', 'true').lower() in ['true', 'on', '1'] MAILUSERNAME = os.environ.get('MAILUSERNAME') MAILPASSWORD = os.environ.get('MAILPASSWORD') ADMINEMAIL = os.environ.get('ADMINEMAIL') # Pagination POSTSPERPAGE = 10 USERSPERPAGE = 50 # Upload configuration MAXCONTENTLENGTH = 16 * 1024 * 1024 # 16MB UPLOADFOLDER = os.path.join(basedir, 'uploads') @staticmethod def initapp(app): pass class DevelopmentConfig(Config): """Development configuration.""" DEBUG = True SQLALCHEMYDATABASEURI = os.environ.get('DEVDATABASEURL') or \ 'sqlite:///' + os.path.join(basedir, 'app-dev.db') class TestingConfig(Config): """Testing configuration.""" TESTING = True SQLALCHEMYDATABASEURI = os.environ.get('TESTDATABASEURL') or 'sqlite://' WTFCSRFENABLED = False class ProductionConfig(Config): """Production configuration.""" SQLALCHEMYDATABASEURI = os.environ.get('DATABASEURL') or \ 'sqlite:///' + os.path.join(basedir, 'app.db') @classmethod def initapp(cls, app): Config.initapp(app) # Log to stderr import logging from logging import StreamHandler file_handl

// original public source
davila7/claude-code-templates
/cli-tool/templates/python/examples/flask-app/.claude/commands/app-factory.md
License: MIT License
Independent project, not affiliated with Anthropic. This skill remains the property of its original author.
// install this skill
Paste this command in your terminal at the root of your project:
mkdir -p .claude/commands && curl -o ".claude/commands/app-factory.md" "https://raw.githubusercontent.com/davila7/claude-code-templates/main/cli-tool/templates/python/examples/flask-app/.claude/commands/app-factory.md"
Then in Claude Code, type /app-factory to activate it.
open_in_newOpen original source
// save
Save available after sign in.
loginSign in to save
// information
Creatordavila7
Stars 30.6k
CategoryBackend
LicenseMIT License
UpdatedMay 22, 2026
Format.md
AccessFree
// similar

Skills Backend

View allarrow_forward