Explain the differences between Flask's app context and request context.

Instruction: Provide a detailed comparison between Flask's application context and request context, including when each is used.

Context: This question tests the candidate's deep understanding of Flask's context mechanisms, which are fundamental for managing global objects and data throughout an application lifecycle.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

The application context makes current_app and g available. The request context makes request and session available and ensures there is an application context for the same app. Neither should be treated as an automatically active process-lifetime container.

A normal request pushes the needed contexts and pops them when handling ends. A CLI command or explicit app.app_context() can have an application context without any HTTP request...

Related Questions