Rol*_*Bly 4 html python jinja2 flask
我的项目(Python 2.7)包含一个屏幕抓取器,它每天收集一次数据,提取有用的内容并将其存储在几个泡菜中.使用Flask/Ninja将泡菜渲染为HTML页面.一切正常,但在我的localhost(Windows 10)上运行时,速度相当慢.我打算在PythonAnywhere上部署它.
该网站还有一个关于页面.about页面的内容是markdown文件,我markdown2在每次编辑后使用它转换为HTML .about-template加载HTML,如下所示:
{% include 'about_content.html' %}
Run Code Online (Sandbox Code Playgroud)
这比加载渲染about-text 要快得多Flask-Markdown(就像我之前一样):
{% filter markdown %}
{% include 'about_content.md' %}
{% endfilter %}
Run Code Online (Sandbox Code Playgroud)
接着.我有点担心在部署网站时主页面加载速度不够快.内容每天只更新一次,如果刷新主页,则无需重新渲染任何内容.所以我想知道我是否可以使用与about-content类似的技巧:
在渲染泡菜之后,我可以让Flask将结果保存为html,然后从部署的站点提供服务吗?或者我可以调用某些浏览器模块,保存其输出并提供服务吗?或者这完全是一个坏主意,我不应该担心因为Flask在现实生活中的服务器上速度快吗?
你可以用Jinja做很多事情.可以随时运行Jinja并将其另存为HTML文件.这样,每次发送文件请求时,都不必再次呈现它.它只是提供静态文件.
这是一些代码.我认为在整个生命周期中都没有改变.因此,一旦创建了视图,我就会创建一个静态HTML文件.
from jinja2 import Environment, FileSystemLoader
def example_function():
'''Jinja templates are used to write to a new file instead of rendering when a request is received. Run this function whenever you need to create a static file'''
# I tell Jinja to use the templates directory
env = Environment(loader=FileSystemLoader('templates'))
# Look for the results template
template = env.get_template('results.html')
# You just render it once. Pass in whatever values you need.
# I'll only be changing the title in this small example.
output_from_parsed_template = template.render(title="Results")
with open("/directory/where/you/want/to/save/index.html", 'w') as f:
f.write(output_from_parsed_template)
# Flask route
@app.route('/directory/where/you/want/to/save/<path:path>')
def serve_static_file(path):
return send_from_directory("directory/where/you/want/to/save/", path)
Run Code Online (Sandbox Code Playgroud)
现在,如果你去,上面的URI localhost:5000/directory/where/you/want/to/save/index.html服务没有渲染.
编辑注意,@app.route需要一个URL,所以/directory/where/you/want/to/save必须从根开始,否则你得到ValueError: urls must start with a leading slash.此外,您可以使用其他模板保存呈现的页面,然后按如下方式进行路由,从而无需(并且速度一样快)send_from_directory:
@app.route('/')
def index():
return render_template('index.html')
Run Code Online (Sandbox Code Playgroud)
如果您想获得更好的性能,请考虑通过gunicorn,nginx等提供Flask应用程序.
Flask还有一个选项,您可以在其中启用多线程.
app.run(threaded=True)
Run Code Online (Sandbox Code Playgroud)