
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
`contextproxy` provides context-based lazy-loaded proxy objects for flask apps.
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.
Flask
Contexts: The decorator works seamlessly with Flask
's request and application contexts, ensuring context isolation and cleanup.You can install contextproxy
by including the file in your project directory or packaging it as a Python module.
pip install .
To 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.
It should be noted that the resource is finalized only after the application context ends (for Flask>=0.9
). That means the resource will be shared across multiple requests within the same application context.
from flask import Flask
from contextproxy import contextproxy
app = Flask(__name__)
@contextproxy(app)
def resource():
# Initialize the resource
resource_value = "This is a shared resource"
yield resource_value
# Teardown logic (e.g., closing connections) goes here
print("Resource has been cleaned up")
@app.route('/')
def index():
return f"Resource: {resource}"
if __name__ == "__main__":
app.run(debug=True)
In 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.
If your resource initialization involves risky operations (like database connections), you can handle exceptions cleanly within the resource function.
@contextproxy(app)
def risky_resource():
uuid = uuid4()
print(f"before: Preparing to create resource ({uuid})")
try:
print(f"yielding: Creating resource ({uuid})")
yield f"resource {uuid=}"
print(f"yielded: where is this? ({uuid})")
except Exception as e:
print(f"except: error processing resource ({uuid}): {type(e)}: {e}")
else:
print(f"else: okey processing resource ({uuid})")
finally:
print(f"finally: Destroying resource ({uuid})")
print(f"after: Destroyed resource ({uuid})")
If you’d like to contribute to contextproxy
, feel free to fork the repository, submit issues, or open a pull request!
This project is licensed under the MIT License.
FAQs
`contextproxy` provides context-based lazy-loaded proxy objects for flask apps.
We found that contextproxy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.