Skip to main content

Flask Context Manager

The Flask Context Manager is a project that provides an inversion of control (IoC) container for Flask applications. It offers features reminiscent of the Spring Boot framework, including dependency injection, route management, configuration reading, and more.

Installation

pip install flask-context-manager

Initialization (Optional)

In the terminal, run the following command to initialize folder structure:

flask_context_manager start

This will create a folder structure so you can add inside your current package.

Features

  • Dependency Injection: Enjoy automatic dependency injection. Classes with @Service, @Controller, or @Component are managed automatically, and their dependencies are resolved via constructors.

  • Route Management: Define routes at the method level using @get_mapping, @post_mapping, @put_mapping, @delete_mapping. The @rest_mapping adds a prefix to all routes in a controller.

  • Dynamic URL Handling: With dynamic URL routing, methods can easily fetch parameters from URLs.

  • POST Method Parameters: Design POST methods effortlessly by specifying parameters directly in the method.

Usage

Structure your application with directories for services (/service), controllers (/controller), and components (/component).

Basic Example

service/hello_service.py

@Service
class HelloService:
    
    def get_hello(self):
        return "Hello, World!"

controller/hello_controller.py

@Controller
@rest_mapping('/api/v1')
class HelloController:
    
    def __init__(self, hello_service: HelloService):
        self.hello_service = hello_service

    @get_mapping('hello')
    def hello(self):
        return self.hello_service.get_hello()

Configuration

Similar to Spring Boot, we can add the @Configuration annotation so that any annotated @Bean methods are automatically registered in the context as soon as it starts.

config/app_config.py

@Configuration
class AppConfig:
    
    @Bean
    def my_jackson_copy(self):
        return MyJacksonCopy()

    @Bean
    def i_just_run_and_return_nothing_and_its_ok(self):
        print("First!!!")

Handling Dynamic URL Parameters:

The Flask Context Manager supports dynamic parameters in routes just like native Flask. To capture a portion of the URL as a variable, you can use the <variable_name> syntax in your mapping.

Example:

controller/user_controller.py

@Controller
@rest_mapping('/api/v1')
class UserController:

    @get_mapping('user/<user_id>')
    def get_user_by_id(self, user_id):
        return f"Fetching info for user with ID: {user_id}"

POST Method with Direct Parameters:

controller/test_controller.py

@Controller
class TestController:

    @post_mapping("/test")
    def my_post(body):
        return "This is a cool body:" + str(body)

Starting the Application:

In the main application file, initiate the Context Manager:

app.py

from flask import Flask
from flask_context_manager.src.main.core.context_manager import ContextManager

app = Flask(__name__)
ContextManager.append(app)

if __name__ == "__main__":
    ContextManager.start()

Requirements

  • Python 3.6+
  • Flask

Note: This project is mainly for educational purposes. Ensure thorough testing and code review before deploying in a production setting.

Metadata

Release files for flask-context-manager 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for flask-context-manager 2.0.0
File Interpreter ABI Platform
flask_context_manager-2.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / flask_context_manager-2.0.0-py2.py3-none-any.whl

Download URL flask_context_manager-2.0.0-py2.py3-none-any.whl
Size 20.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e1bf5bb677cba88514e120b06c7efeaf10a27e91396374fc3cd79ba07be95807
BLAKE2b-256 checksum
How to use checksums
c0ef8051f9b6756acade64d570bee8f05d29550b66020764665b6745f7511de7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Release history Release notifications | RSS feed

This release

2.0.0 This release

1 release file

1.2.3

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.10

1 release file

1.1.9

1 release file

1.1.8

1 release file

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.13

1 release file

1.0.12

1 release file

1.0.11

1 release file

1.0.10

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page