Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Creating your own local proxies

Posted by LeafStorm on 2010-05-07 @ 22:20 and filed in Utilities

Sometimes, you don't want to prefix whatever you are sharing in your thread local with g. for some reason. To solve this problem, you can create your own local-type proxy objects with:

from werkzeug.local import LocalProxy

whatever = LocalProxy(lambda: g.whatever)

Of course, this can be used for other things besides just g.

from werkzeug.local import LocalProxy

method = LocalProxy(lambda: request.method)

Now that Werkzeug 0.6.1 is out, it's really more of an AnythingProxy than just a LocalProxy.

This snippet by LeafStorm can be used freely for anything you like. Consider it public domain.