使用Python开发Flask应用时如何自动刷新浏览器? [英] How to automate browser refresh when developing an Flask app with Python?
问题描述
我已经开始学习Flask来开发Web应用程序.我真正想念的是任何代码更改(包括静态文件,模板等)后,浏览器会自动刷新.这似乎是几乎所有Javascript框架中的标准功能.前端人员对此有以下几个术语:自动重载/刷新,热重载/刷新(hotreload),实时重载/刷新(livereload),...
I've started learning Flask to develop web applications. What I am realy missing is an automatic Browser refresh after any code change (including static files, templates, etc.). This seems to be a standard feature in almost any Javascript framework. Frontend people have several terms for that: auto reload / refresh, hot reload / refresh (hotreload), live reload / refresh (livereload), ...
在Stackoverflow上,最相似的问题与Flask服务器的自动重载有关(-> https://stackoverflow.com/search?q=flask+auto+reload ).
Here on Stackoverflow most similar questions are related to the auto-reload of the Flask server (--> https://stackoverflow.com/search?q=flask+auto+reload).
J只希望刷新浏览器即可.
我用Google搜索并尝试了几件事-没运气:
I googled and tried several things - with no luck:
- https://github.com/lepture/python-livereload
- https://gist.github.com/amontalenti/8922609
我如何能在使用Flask时获得流畅的开发体验,而不必每天在浏览器中按F5键1000次才能查看更改结果?
How can I have a smooth developing experience with Flask without having to hit the F5 key 1000 times a day in a browser just to see the results of my changes?
我认为答案与上面链接中的python-livereload
相近.
所以我想我的问题的另一个标题可能是:
I think the answer is somewhere near to python-livereload
from the link above.
So I guess an alternative title of my question could be:
有人可以使用Flask + python-livereload吗?
我很愚蠢,无法从他们的文档中获取它:)
I am to dumb to get it from their documentation :)
为了完整起见,这里是我正在使用的Flask应用程序.
for the sake of completenes here is the Flask app I am using.
# filename: main.py
from flask import Flask, render_template
from livereload import Server
app = Flask(__name__)
@app.route('/')
def index():
return "INDEX"
@app.route('/bart')
def use_jinja():
return render_template('basic.html')
if __name__ == '__main__':
server = Server(app.wsgi_app)
server.serve(port=5555)
我通过以下方式启动应用
I start the app with
python main.py
推荐答案
您提出了一个有趣的问题,因此我构建了一个使用livereload库的快速且肮脏的Flask应用程序.下面列出了使它正常工作的关键步骤:
This is an interesting question you've raised so I built a quick and dirty Flask application which utilizes the livereload library. Listed below are the key steps for getting this to work:
-
下载并安装livereload库:
Download and install the livereload library:
pip install livereload
在启动烧瓶应用程序的主文件中,在我的特殊情况下,run.py
用livereload提供的Server
类包装烧瓶应用程序.
Within your main file which starts up your flask application, run.py
in my particular case, wrap the flask application with the Server
class provided by livereload.
例如,我的run.py
文件如下所示:
For example, my run.py
file looks like the following:
from app import app
from livereload import Server
if __name__ == '__main__':
server = Server(app.wsgi_app)
server.serve()
-
再次启动服务器:
Start your server again:
python run.py
在浏览器中导航到localhost,您的代码更改将自动刷新.对我来说,我使用了livereload提供的默认端口5500,因此我的网址如下所示:http://localhost:5500/
.
Navigate to localhost in the browser and your code changes will be auto-refreshed. For me I took the default port of 5500 provided by livereload so my url looks like the following: http://localhost:5500/
.
通过这些步骤,您现在应该可以在Python开发中利用自动重载的功能,类似于webpack为大多数前端框架提供的功能.
With those steps you should now be able to take advantage of auto-reloads for your python development, similar to what webpack provides for most frontend frameworks.
为完整性起见,可以在此处找到
For completeness the codebase can be found here
希望有帮助!
这篇关于使用Python开发Flask应用时如何自动刷新浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!