Constrictor is a microframework based on Flask, designed to simplify the creation and management of modular Flask applications. With features inspired by Rails, Constrictor allows for easy module generation, testing, and execution using YAML-based templates for maximum flexibility.
- Modular Structure: Organize your Flask app into separate modules with ease.
- Blueprint Support: Seamless integration with Flask Blueprints.
- YAML Templates: Generate modules using declarative YAML templates for maximum flexibility and customization.
- Command-Line Interface: Simplified commands for creating projects, generating modules, running the app, and testing.
- Environment Configuration: Load configurations from
.envfiles and environment variables. - Automated Testing: Built-in test generation and execution for all modules.
Install Constrictor via pip:
pip install constrictorConstrictor follows a modular architecture where each module is self-contained with its own routes, models, views, templates, and tests. The framework uses YAML templates to define the structure and content of generated modules.
When you create a project with Constrictor, it generates the following structure:
project_name/
├── app.py # Main Flask application
├── __init__.py # Package initialization
├── .env.example # Environment variables template
├── .gitignore # Git ignore file
├── requirements.txt # Python dependencies
├── templates/ # HTML templates (centralized)
│ └── module_name/
│ └── index.html
├── modules/ # Application modules
│ └── module_name/
│ ├── __init__.py
│ ├── routes.py # Blueprint routes
│ ├── models.py # Data models
│ ├── views.py # View functions
│ ├── tests/ # Module tests
│ │ └── test_module_name.py
│ ├── views/ # Additional views
│ ├── models/ # Additional models
│ └── static/ # Static assets
└── .venv/ # Virtual environment
graph TB
subgraph "Constrictor Framework"
CLI[CLI Commands]
YAML[YAML Templates]
Parser[YAML Parser]
Loader[Blueprint Loader]
end
subgraph "Generated Project"
App[Flask App]
Modules[Modules Directory]
Templates[Templates Directory]
Tests[Test Suite]
end
subgraph "Module Structure"
Routes[Routes.py]
Models[Models.py]
Views[Views.py]
ModuleTests[Module Tests]
Static[Static Assets]
end
CLI --> YAML
CLI --> Parser
YAML --> Parser
Parser --> Modules
Parser --> Templates
Parser --> Tests
App --> Loader
Loader --> Modules
Modules --> Routes
Modules --> Models
Modules --> Views
Modules --> ModuleTests
Modules --> Static
Routes --> Templates
Views --> Templates
Models --> Routes
-
Create a New Project:
constrictor new project_name
-
Generate a New Module:
constrictor generate module_name # or use the alias constrictor g module_nameGenerate with Custom Template:
constrictor generate module_name --template custom_template.yml
-
Run the App:
constrictor run
-
Run Tests for All Modules:
constrictor test -
Run Tests for Specific Modules:
constrictor test module1 module2
Constrictor uses YAML templates to define the structure and content of generated modules. This approach provides maximum flexibility and allows for easy customization.
The default YAML template includes:
- Routes: Define API endpoints and web routes
- Templates: HTML templates with Jinja2 support
- Tests: Automated test generation
- Models: Data models and business logic
- Views: View functions and controllers
You can create custom YAML templates to suit your specific needs:
module:
name: "{{module_name}}"
description: "Custom module template"
routes:
- path: "/{{module_name}}/"
method: "GET"
function: "index"
template: "{{module_name}}/index.html"
response_type: "html"
templates:
- name: "index.html"
path: "{{module_name}}/index.html"
content: |
<!DOCTYPE html>
<html>
<head>
<title>{{module_name|title}} Module</title>
</head>
<body>
<h1>{{module_name|title}} Module</h1>
</body>
</html>Templates support Jinja2 syntax with the following variables:
{{module_name}}: The name of the module being generated{{module_name|title}}: Capitalized module name- Custom variables can be added to the template context
constrictor new <project_name>: Create a new Constrictor projectconstrictor generate <module_name>: Generate a new module using YAML templatesconstrictor generate <module_name> --template <template.yml>: Generate module with custom template
constrictor run: Start the development serverconstrictor run --host <host> --port <port>: Start server with custom host/portconstrictor run --debug: Start server in debug mode
constrictor test: Run all testsconstrictor test <module1> <module2>: Run tests for specific modules
Constrictor requires the following Python packages:
- Flask: Web framework
- Click: Command-line interface
- PyYAML: YAML template processing
- pytest: Testing framework
- pytest-flask: Flask testing utilities
- python-dotenv: Environment variable management
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License.
In construction.