创建flask项目
1)创建虚拟环境
2)打开pycharm,选择flask。选择之前的虚拟环境接口
3)运行一个hello world
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
结果:
flask启动的基本配置的使用:
# -*- coding: utf-8 -*- from flask import Flask app = Flask( __name__ )
#开启调试模式 app.config['DEBUG'] = True @app.route( "/" ) def hello(): return "Hello, I Love Imooc " if __name__ == "__main__":
#设置监听的主机为所有 app.run( host = "0.0.0.0" ) 当然也可以直接使用:
app.run(host="0.0.0.0",debug=True)
加载模块的导入方式:
app.config.from_object( "config.base_setting" )
#app.config.from_pyfile( "config/base_setting.py" )也可以这样写
@app.route( "/" )
def hello():
return "Hello, I Love Imooc "
if __name__ == "__main__":
app.run( host = "0.0.0.0" )
在app.config模块[名字:base_setting]中的内容:
DEBUG = True
环境变量加载模式
# -*- coding: utf-8 -*-
from flask import Flask
app = Flask( __name__ )
app.config.from_envvar( "ops_config" )
@app.route( "/" )
def hello():
return "Hello, I Love Imooc "
if __name__ == "__main__":
app.run( host = "0.0.0.0" )
注意在配置环境变量加载时:local表示你添加环境变量的参数的文件的路径。此处是:set ops_config=./config/base_setting.py