搭建flask工程
搭建虚拟环境
pip install virtualenv
virtualenv flaskdemo
cd flaskdemo\Scripts\
activate
pip install flask
pycharm 配置虚拟环境
在files -> settings中,将解释器配置项更改为D:\flaskdemo\flaskdemo\Scripts\python.exe即可
运行使用
创建一个helloworld.py文件,输入以下代码:
from flask import Flask
app = Flask(__name__)
class DefaultConfig(object):
SECRET_KEY = "TPmi4asfgwrfergfegr2dWYW1"
app.config.from_object(DefaultConfig)
@app.route("/")
def index():
print(app.config["SECRET_KEY"])
return "hello world"
if __name__ == '__main__':
app.run()
运行后,即可访问 http://127.0.0.1:5000/
若没有app.run(),怎么运行呢? 方式如下:
set FLASK_APP=helloworld
flask run
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
flask run -h 0.0.0.0 -p 8000
flask run --help
? export FLASK_ENV=production 运行在生产模式,未指明则默认为此方式
? export FLASK_ENV=development 运行在开发模式
export FLASK_APP=helloworld
python -m flask run
* Running on http://127.0.0.1:5000/
export FLASK_APP=helloworld
flask run
* Running on http://127.0.0.1:5000/
|