今天在看flask的官方教程在第一部分,创建应用这里遇到一个问题,他在__init__.py创建了一个create_app的函数,然后写了一些配置信息,我并没有看到他调用,它是怎么跑起来的?我知道__init__.py这个文件会自动运行,但里面的创建的函数也会自动运行吗?官方代码如下

mkdir flaskr

flaskr/__init__.py

import os

from flask import Flask

def create_app(test_config=None):

# create and configure the app

app = Flask(__name__, instance_relative_config=True)

app.config.from_mapping(

SECRET_KEY='dev',

DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),

)

if test_config is None:

# load the instance config, if it exists, when not testing

app.config.from_pyfile('config.py', silent=True)

else:

# load the test config if passed in

app.config.from_mapping(test_config)

# ensure the instance folder exists

try:

os.makedirs(app.instance_path)

except OSError:

pass

# a simple page that says hello

@app.route('/hello')

def hello():

return 'Hello, World!'

return app

export FLASK_APP=flaskr

export FLASK_ENV=development

flask run

这样就跑起来了,但是它是怎么跑起来的create_app是怎么调用的?

谢谢!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐