1.flask基础
# 引入Flask库 from flask import Flask # 实例化,创建对象 app = Flask(__name__) # 设置路由 @app.route(‘/‘) # 定义视图函数 def index(): return ‘Hello World, this is my first flask web app!‘ if __name__ == ‘__main__‘: app.run()
2.flask初始化参数
# 引入Flask库 from flask import Flask # 实例化,创建对象 # app = Flask(__name__) app=Flask(import_name=__name__,static_url_path="/s",static_folder="static_file",template_folder="template_file") # 设置路由 @app.route(‘/‘) # 定义视图函数 def index(): return ‘Hello World, this is my first flask web app!‘ if __name__ == ‘__main__‘: app.run()
3.应用程序配置参数
3.1 从配置对象中加载
# 引入Flask库 from flask import Flask class DefaultConfig(object): SECRETE_KEY="abcd" #必须大写 # 实例化,创建对象 app = Flask(__name__) #设置 app.config.from_object(DefaultConfig) # 设置路由 @app.route(‘/‘) # 定义视图函数 def index(): print(app.config["SECRETE_KEY"]) return ‘Hello World, this is my first flask web app!‘ if __name__ == ‘__main__‘: app.run()
3.2 从配置文件中加载
# 引入Flask库 from flask import Flask # 实例化,创建对象 app = Flask(__name__) #设置 app.config.from_pyfile("setting.py") # 设置路由 @app.route(‘/‘) # 定义视图函数 def index(): print(app.config["SECRETE_KEY"]) return ‘Hello World, this is my first flask web app!‘ if __name__ == ‘__main__‘: app.run()
3.3 从环境变量中加载
# 引入Flask库 from flask import Flask # 实例化,创建对象 app = Flask(__name__) #设置环境变量 export PROJECT_SETTING="setting.py" app.config.from_envvar("PROJECT_SETTING",silent=True) # 设置路由 @app.route(‘/‘) # 定义视图函数 def index(): print(app.config["SECRETE_KEY"]) return ‘Hello World, this is my first flask web app!‘ if __name__ == ‘__main__‘: app.run()