Skip to content

Instantly share code, notes, and snippets.

@catermelon
Last active January 15, 2022 20:11
Show Gist options
  • Save catermelon/9137101 to your computer and use it in GitHub Desktop.
Save catermelon/9137101 to your computer and use it in GitHub Desktop.
from flask import current_app
class FeatureFlags(object):
def __init__(self, app=None):
if app is not None:
self.init_app(app)
def init_app(self, app):
app.config.setdefault('FEATURE_FLAGS’, {})
if hasattr(app, "add_template_test"):
app.add_template_test(self.in_config, name='active_feature')
else:
app.jinja_env.tests['active_feature'] = self.in_config
app.extensions['FeatureFlags'] = self
def in_config(self, feature):
try:
return current_app.config['FEATURE_FLAGS'][feature]
except (AttributeError, KeyError):
return False
def is_active(feature):
feature_flagger = current_app.extensions['FeatureFlags']
return feature_flagger.in_config(feature)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment