Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Flask Context – Demystifying Application and Request Contexts

Flask Context

In this tutorial, we will first introduce Flask Contexts and then further look into the two contexts in Flask – Application Context and Request Context.

What is a Flask Context?

Flask uses Context to make certain variables globally accessible, on a temporary basis

In Flask, you might have noticed that the Flask Views don’t take request object as an argument and can still use them. It can be possible only if request objects are global objects, right?

Well, the answer is No.

If the request objects were to be made global, then Flask won’t distinguish between the requests that hit the server simultaneously. But that is not the case; websites does handle multiple requests at the same time. Then how is it possible??

Well, Flask uses Context to make certain variables temporarily global for a particular request object so that the Views can access those variables to return the output.

Flask context is of two types:

  • Application Context
  • Request Context

Application Contexts in Flask

The application context keeps track of the application-level data. Hence these store values specific to the whole application like the database connections, configurations, etc.

The Application Context exposes (ie, temporarily make them global) objects such as the current_app and a g variable.

1. current_app

current_app refers to the instance handling the requests. That is, it relates to the application Flask is running on.

2. g variable

Here g stands for Global and is used to store data like the database details etc during request handling temporarily.

Once the values for current_app and g variables are set, any View inside the application can use them.

Flask pushes(or activates) the Application Context automatically when a particular request comes in and removes it once the request is handled.

Request Context in Flask

Similar to the Application Context, The request context keeps track of the request-level data. Hence these store values that are specific to each request.

Request Context exposes objects like requests and the sessions.

1. Requests

The request object contains information about the current web request. The request context makes requests temporarily global due to which all the Views can easily access them without taking them as arguments.

Note: requests contains information only of the current request. When a new request comes in, the request object stores information about the current new request and the previous information is deleted.

2. Sessions

A session is a dictionary-like object that stores the information which persists between requests, unlike the request object. There will also be an entirely different article on Flask’s sessions soon on our website to give you better information. 

Hence once the request context is made active, any View inside the application can access objects (request and sessions) exposed by it.

Like the Application Context, Flask also pushes(or activates) the request context automatically when a particular request comes in, and removes it once the request is handled. 

Note: When a Request Context is pushed, it automatically creates an Application Context as well if it is not present already.

Manually Pushing Flask Context in the shell.

Flask application creates/pushes the Application and the request Contexts automatically. 

Hence, inside the view functions, you can access all the objects exposed by application and request without worrying about the Contexts.

However, if you try to use the objects outside the View Function or in python shell as shown below:

from flask import Flask, request
request.method
Error
Error

You will get an error. Same with the application context objects

from flask import Flask, current_app
current_app.name
Error
Error

This is because the application and the request context are not active. Therefore we first have to create them.

Here, We create the application context using the app_context() method of Flask Instance

Run the code:

from flask import Flask, current_app
app = Flask(__name__)
appli_context = app.app_context()
current_app.name

Here

  • We declare a Flask object – app.
  • We push/create a application context using app.app_context()
  • current_app is now active and is linked to the __name__ file i.e. the __main__ file itself.
App Context
App Context

See now the error is gone! Similarly, we create the request context using the test_request_context() method of Flask Instance

from flask import Flask, request
app = Flask(__name__)
req = app.test_request_context()
req.request

Here as well

  • We declare a Flask object – app.
  • We push/create a request context using app.tes_request_context()
  • The request object is now active and is linked to the host website ie, the ” http://loalhost/ ” file itself.
Request
Request

And hence we get a proper error free output.

Conclusion

That’s it, Guys !! That was all about Contexts in Flask. You don’t need to worry much about it since Flask creates them automatically inside the application file.