{"id":23811767,"url":"https://github.com/guoquan/contextproxy","last_synced_at":"2026-02-28T15:01:04.492Z","repository":{"id":259979766,"uuid":"859686739","full_name":"guoquan/contextproxy","owner":"guoquan","description":"`contextproxy` provides context-based lazy-loaded proxy objects for flask apps","archived":false,"fork":false,"pushed_at":"2024-09-20T16:24:11.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-06T16:31:32.614Z","etag":null,"topics":["flask"],"latest_commit_sha":null,"homepage":"https://github.com/guoquan/contextproxy","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/guoquan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-19T05:30:48.000Z","updated_at":"2024-09-20T16:24:03.000Z","dependencies_parsed_at":"2024-10-28T22:38:17.855Z","dependency_job_id":"c7777f69-d3cf-43de-8011-c4ed611c80ad","html_url":"https://github.com/guoquan/contextproxy","commit_stats":null,"previous_names":["guoquan/contextproxy"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/guoquan/contextproxy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guoquan%2Fcontextproxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guoquan%2Fcontextproxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guoquan%2Fcontextproxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guoquan%2Fcontextproxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guoquan","download_url":"https://codeload.github.com/guoquan/contextproxy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guoquan%2Fcontextproxy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29938962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T13:49:17.081Z","status":"ssl_error","status_checked_at":"2026-02-28T13:48:50.396Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask"],"created_at":"2025-01-02T01:33:22.263Z","updated_at":"2026-02-28T15:01:04.473Z","avatar_url":"https://github.com/guoquan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask Context Proxy\n\n`contextproxy` is a `@contextmanager` style `LocalProxy`, managed by `flask.g`, designed to simplify the management of lazily loaded, context-based resources in `Flask` applications. It allows resources to be easily accessed, automatically initialized and cleaned up based on `Flask`'s request and application lifecycle, and can be used to share resources across multiple requests or manage them on a per-request basis.\n\n[![Release](https://github.com/guoquan/contextproxy/actions/workflows/release.yml/badge.svg)](https://github.com/guoquan/contextproxy/actions/workflows/release.yml)\n[![PyPI - Version](https://img.shields.io/pypi/v/contextproxy)](https://pypi.org/project/contextproxy/)\n[![GitHub License](https://img.shields.io/github/license/guoquan/contextproxy)](LICENSE)\n\n## Features\n\n- **Easy Access**: Resources can be accessed using decorated names, making them easy to use in your application.\n- **Lazy Initialization**: Resources are only initialized when accessed, saving computation and memory for unused resources.\n- **Automatic Teardown**: Resources are cleaned up automatically after the application context is torn down.\n- **Supports `Flask` Contexts**: The decorator works seamlessly with `Flask`'s request and application contexts, ensuring context isolation and cleanup.\n- **Thread Safety**: Ensures that resources are unique per thread in multi-threaded environments.\n\n## Installation\n\nYou can install `contextproxy` by including the file in your project directory or packaging it as a Python module.\n\n```bash\npip install .\n```\n\n## Usage\n\nTo use `contextproxy`, simply apply it as a decorator to a generator function that yields the resource you want to manage. The resource will be lazily initialized and binded to `flask.g` for the duration of the application context.\n\nIt should be noted that the resource is finalized only after the application context ends (for `Flask\u003e=0.9`). That means the resource will be shared across multiple requests within the same application context.\n\n### Basic Example\n\n```python\nfrom flask import Flask\nfrom contextproxy import contextproxy\n\napp = Flask(__name__)\n\n@contextproxy(app)\ndef resource():\n    # Initialize the resource\n    resource_value = \"This is a shared resource\"\n    yield resource_value\n    # Teardown logic (e.g., closing connections) goes here\n    print(\"Resource has been cleaned up\")\n\n@app.route('/')\ndef index():\n    return f\"Resource: {resource}\"\n\nif __name__ == \"__main__\":\n    app.run(debug=True)\n```\n\nIn the example above, the `resource` is lazily initialized the first time it's accessed and will be automatically cleaned up after the application context ends.\n\n## Advanced Usage\n\n### Handling Exceptions in Resource Initialization\n\nIf your resource initialization involves risky operations (like database connections), you can handle exceptions cleanly within the resource function.\n\n```python\n@contextproxy(app)\ndef risky_resource():\n    uuid = uuid4()\n    print(f\"before: Preparing to create resource ({uuid})\")\n    try:\n        print(f\"yielding: Creating resource ({uuid})\")\n        yield f\"resource {uuid=}\"\n        print(f\"yielded: where is this? ({uuid})\")\n    except Exception as e:\n        print(f\"except: error processing resource ({uuid}): {type(e)}: {e}\")\n    else:\n        print(f\"else: okey processing resource ({uuid})\")\n    finally:\n        print(f\"finally: Destroying resource ({uuid})\")\n    print(f\"after: Destroyed resource ({uuid})\")\n```\n\n## **Contributing**\n\nIf you’d like to contribute to `contextproxy`, feel free to fork the repository, submit issues, or open a pull request!\n\n## **License**\n\nThis project is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguoquan%2Fcontextproxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguoquan%2Fcontextproxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguoquan%2Fcontextproxy/lists"}