How to run function before Flask routing is starting?

前端 未结 1 1026
情书的邮戳
情书的邮戳 2021-01-26 04:53

I need to do call function before Flask routing is starting working. Where I should to put function to make it called at service start. I did:

app = Flask(__name         


        
相关标签:
1条回答
  • 2021-01-26 05:15

    If i were you, I'd put it in a function creating an app, like:

    def checkIfDBExists(): # it is my function
        if not DBFullPath.exists():
             print("Local DB do not exists")
        else:
             print("DB is exists")
    
    def create_app():
        checkIfDBExists()
        return Flask(__name__)
    
    app = create_app()
    

    This will allow you to perform any necessary steps when you discover any settings are wrong. You can also perform routing in that function. I've written such function to separate this process here:

    def register_urls(app):
        app.add_url_rule('/', 'index', index)
        return app
    
    0 讨论(0)
提交回复
热议问题