最近一直在学习python,学到了flask框架。
我照着文档创建了hello.py文件
from html import escape
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % escape(username)
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return 'Subpath %s' % escape(subpath)
if __name__ == '__main__':
app.run()
写入了一些基本代码,但是发现无法热更新,每次修改都要重新启动一次flask,很麻烦
接着百度了一下方法,得到三种方法
#第一种
#在app.run()加入参数
if __name__ == '__main__':
app.run(debug=True)
#第二种
#修改app.config['DEBUG']的值
app.config['DEBUG'] = True
#第三种
#添加configs.py文件,文件内容:DEBUG = True,然后在hello.py加上下面的代码
import configs
app.config.from_object(configs)
但是这三种都无效,可能是因为这个是那个pycharm的,我没有用这个所以无效
接着我仔细看来下文档发现还可以通过命令行开启
#linux
#这个我没测试过不知道行不行
export FLASK_APP=hello.py
export FLASK_ENV=development
flask run
#Windows
#这个我也测试了不行,可能跟命令行工具有关,我用的是powershell,这个有特殊的设置方式
set FLASK_APP=hello.py
set FLASK_ENV=development
set FLASK_DEBUG=1
flask run
#powershell
#这个就可以
$env:FLASK_APP = "hello.py"
$env:FLASK_DEBUG = 1
flask run