Flask Webcast #2: Request and Application Contexts
Posted byon under
In this live webcast I explain what is the purpose of the request and application contexts in Flask, and why sometimes you get these weird errors about them.
Become a Patron!
Hello, and thank you for visiting my blog! If you enjoyed this article, please consider supporting my work on this blog on Patreon!
#1 fbu said 2018-04-28T18:30:19Z
Thanks a million for this video, much appreciated!
One question: the contexts seem to be looked up via Werkzeug utilities. Is that still true if we serve our app through a different server as eg gunicorn?
Thanks for your help and best wishes
#2 Miguel Grinberg said 2018-04-29T16:26:56Z
@fbu: Yes. The support for the context implementation is in Werkzeug, but it is separate from the Werkzeug web server and works regardless of the web server that you use.
#3 pwabbo said 2020-08-15T16:34:06Z
Hi, is it possible that the video https://www.youtube.com/watch?v=Z4X5Oddhcc8 has disappeared?
Youtube seems to be giving a HTTP Error 410: Gone
#4 Miguel Grinberg said 2020-08-19T22:29:47Z
@pwabbo: I have reported it to youtube several days ago, but I'm still waiting for it to be restored. Sorry about that, I depend on YouTube to fix this.
#5 Eduard said 2020-10-07T02:44:32Z
Can you please explain how to access the application context using an init.py and main.py with a blueprint?
I am running BackgroundScheduler to run a DB job at a specific time, but cannot figure out how to connect to DB inside the thread.
#6 Miguel Grinberg said 2020-10-07T08:54:20Z
@Eduard: background jobs have nothing to do with blueprints, so you would be using the same instructions I show in this video to create an app context in your background thread.
#7 Eduard said 2020-10-07T14:00:48Z
Miguel, thanks for the quick response. Unfortunately, I am new to Python and I am not sure that I understand. I declare and start my background thread in main.py file where is only the blueprint, but I do not have access to the app object, and when I import the current_app object it does not work. I guess I understand the concept, I just do not know how to practically do it, and I did not see it explicitly done in your video.
#8 Miguel Grinberg said 2020-10-07T18:35:33Z
@Eduard: The current_app variable does not know the application instance on its own, someone needed to have pushed a context before, as I show in the video. You are using this variable with nobody pushing a context ahead of its use, and that is why you get the error.
You need to figure out a way to pass the app instance to your thread. It basically comes down to that. Either move your thread code to a place where you have access to the app instance, or add a mechanism by which the app instance can be given to the thread once it is known.
#9 Anthony Rogers said 2021-10-10T05:16:56Z
Thanks Miguel. This was really helpful. The bit at the end that discussed the different extension initialization methods and the implications of each is really good to know.