Sometimes you would like to have code that will be executed once, before ant user arrives to your site, before the first request arrives. You could put some code in the body of your Flask application file, that code will execute when the application launches. Alternatively, and in many case in a cleaner way you could use the before_first_request hook.


from flask import Flask

app = Flask(__name__)

def before_first_request():"before_first_request")

def main():"main route")
    return "Hello World!"

Run as:

FLASK_APP=app FLASK_DEBUG=1 flask run

Output on the console:

[2020-06-22 23:24:56,675] INFO in app: before_first_request
[2020-06-22 23:24:56,675] INFO in app: main route - - [22/Jun/2020 23:24:56] "GET / HTTP/1.1" 200 -