of the stack • The object on the top has some internal attributes • You can however add new attributes there • _request_ctx_stack.top.request: current request object
Flask(__name__) @app.before_request def connect_to_db_on_request(): g.db = connect_to_db(app.config['DATABASE_URL']) @app.teardown_request def close_db_connection_after_request(error=None): db = getattr(g, 'db', None) if db is not None: db.close()
Flask(__name__) def get_db(): ctx = _app_ctx_stack.top con = getattr(ctx, 'myapp_database', None) if con is None: con = connect_to_database(app.config['DATABASE_URL']) ctx.myapp_database = con return con @app.teardown_appcontext def close_database_connection(error=None): con = getattr(_app_ctx_stack.top, 'myapp_database', None) if con is not None: con.close()
get_db(): ctx = _app_ctx_stack.top con = getattr(ctx, 'myapp_database', None) if con is None: con = connect_to_database(ctx.app.config['DATABASE_URL']) ctx.myapp_database = con return con def close_database_connection(error=None): con = getattr(_app_ctx_stack.top, 'myapp_database', None) if con is not None: con.close()
= Flask(__name__) >>> @app.teardown_appcontext ... def print_something_on_teardown(error=None): ... print 'I am tearing down:', error ... >>> with app.app_context(): ... print 'This is with the app context' ... This is with the app context I am tearing down: None
am tearing down: integer division or modulo by zero Traceback (most recent call last): File "<stdin>", line 2, in <module> ZeroDivisionError: integer division or modulo by zero
request which is read only • That request object is available through a context local • Response objects on the other hand are passed down the call stack • … can be implicitly created • … can be replaced by other response objects
value from a view function into a response is performed by flask.Flask.make_response • A helper function called flask.make_response is provided that can handle both cases in which you might want to invoke it.
make multiple apps since those applications share nothing. • But what if you want to share some things between apps? • For instance an app that shares everything with another one except for the configuration settings and one view.
• Flask extensions do not use a plugin system • They can modify the Flask application in any way they want • You can use decorators, callbacks or blueprints to implement them
until the dispatcher returns • That means until the response object was constructed • In debug mode there is an exception to that rule: if an exception happens during request handling the context is temporarily kept around until the next request is triggered.
it might be inconvenient if the context disappears when the function returns. • flask.stream_with_context is a helper that can keep the context around for longer.
None try: for item in gen: yield item finally: if hasattr(gen, 'close'): gen.close() wrapped_g = wrap_gen() wrapped_g.next() return wrapped_g Built into Flask 0.9
salt or secret key • Also you can put more information into the data you're dumping to make it expire with certain conditions (for instance md5() of password salt. If password changes, redeem link gets invalidated) • For user activation: don't activate if user already activated.
you're comfortable with • If you run into scaling problems you are a lucky person • Smaller pieces of independent code are easier to optimize and replace than one monolithic one