Replacing before_first_request #4877
Replies: 3 comments 2 replies
-
First, your secret key should be obtained from the environment some way (whether as an environment variable, from a config file, etc.). So your other library could equally well just load the secret key from the same place your app does. Second, and regardless of the above point, I don’t see why you can’t just do what you are describing in an app factory method. |
Beta Was this translation helpful? Give feedback.
-
Oh, and regardless of how I change things on my end, it does mean also changing the API for my code, which will lead to more panic and confusion on the part of consumers of the library. Fortunately there's not that many users right now, but I also would prefer to make the change in a way which isn't too disruptive to others, and switching from a constructor to a factory is pretty disruptive. |
Beta Was this translation helpful? Give feedback.
-
I also get into trouble with the deprecation of before_first_request. |
Beta Was this translation helpful? Give feedback.
-
Flask's
before_first_request
is being deprecated, due to some fairly valid reasoning. I totally get the reasoning behind it, and I even agree with it!However, I have a Flask-based publishing framework that relies on its behavior. In particular, my framework wraps Flask via inheritance. This framework makes use of another library which needs to be able to access
app.secret_key
at the time of its own instantiation, and also callapp.add_url_rule
before the first request is handled. In effect, this code happens in my framework's__init__
:and then to keep things simple, my framework's setup looks like:
Since
OtherLibrary
's constructor needs to be able to obtain the already-setapp.secret_key
as part of its setup, and needs to be able toadd_url_rule
on the Flask application before the first request is served, this setup can't happen conditionally in the first request'sbefore_request
handler, and I'd rather not have to add another method to ExtendedApp likefinish_setup()
or the like.Also, I had originally not used inheritance (instead providing a library function that generated and configured a Flask application), but for some reason this ended up not being workable for other reasons (I forget what, precisely, but I suspect it had to do with the startup behavior of the ORM I'm using).
So, my feature request is to bring back
before_first_request
, possibly renaming it to give it a better sense of "please don't use this if you don't have to."Thanks.
Beta Was this translation helpful? Give feedback.
All reactions