Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask deprecated before_first_request how to update

Tags:

python

flask

I'm learning web development for simple applications and I've created one that uses before_first_request decorator. According with the new release notes, the before_first_request is deprecated and will be removed from Flask 2.3:

Deprecated since version 2.2: Will be removed in Flask 2.3. Run setup code when creating the application instead.

I don't understand how I can update my code to be complacent with Flask 2.3 and still run a function at first request without using before_first_request. Could some kind soul give me an example?

like image 393
Carlos Avatar asked Aug 31 '25 14:08

Carlos


1 Answers

In place of the @app.before_first_request decorated function like this previously:

@app.before_first_request
def create_tables():
    db.create_all()
    ...

Replace that with this instead.

with app.app_context():
    db.create_all()
    ...

Refer to documentation section on Manually Push a Context.

like image 92
Enkum Avatar answered Sep 15 '25 05:09

Enkum