Skip to content

Instantly share code, notes, and snippets.

@senaps
Created October 6, 2019 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senaps/b168c5b1de7e54bc3854a6245103e4db to your computer and use it in GitHub Desktop.
Save senaps/b168c5b1de7e54bc3854a6245103e4db to your computer and use it in GitHub Desktop.
flask context
@contextmanager
def get_app_context(conf=None):
"""get an app context!
we will receive the name of the application and then try to build out the
applicaton and return it. the important thing with this function is that we
will return the application context not the actual application object.
this is usefull for modules and functions that need the application context
to be able to do what they would need to do.
the config is the name of the config class we want to use to create the app
with.
:param conf: name of the config class
:return: a flask application context
"""
if not conf:
conf = 'development'
try:
app = create_app(cm.get(conf))
with app.app_context() as ctx:
yield ctx
finally:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment