如何在不使用请求上下文的情况下在烧瓶中渲染模板 [英] how to render template in flask without using request context
问题描述
更新:
这是我正在使用的代码
from flask导入Flask,render_template,request,flash,redirect,url_for
import flask
导入时间$ b $ from flask.ext.assets import Environment ,从flask_wtf导入
导入从wtforms导入表格
从wtforms.validators中导入TextField,TextAreaField,SubmitField
import InputRequired
$ b $ CSRF_ENABLED = True
app = Flask (__name__)
app.secret_key ='development key'
app = flask.Flask('my app')
assets = Environment(app)
assets.url = app。 static_url_path
scss = Bundle('scss / app.scss',filters ='scss',output ='css / app.css')
assets.register('app_scss',scss)
@ app.route('/')
def server_1():
r = 1
g = 2
b = 3
i = g
if i == g:
with app.app_context():
print加载模板...
rendered = flask.render_template('server_1.html',green = True)
print成功!
time.sleep(5)
if __name__ =='__main__':
app.run(port = 5000,debug = True)
然后你可以使用 render_template()
从你的模板目录或 render_template_string()
pre>
$ b $ Flask('my app')
with app.app_context():
context = {'name':'bob','age':22}
rendered = flask .render_template('index.html',** context)
with app.app_context():
template ='{{name}}是{{age}}岁。
context = {'name':'bob','age':22}
rendered = flask.render_template_string(template,** context)
$ p $ import jinja2
template = jinja2.Template('{{ ')
rendered = template.render(name ='Ginger',age = 10)
更新
看起来您可能希望将内容重新传输到请求客户。如果是这样,你可以写一个发电机。像这样的东西可能工作:
进口时间$ b $从瓶子进口Flask,Response,render_template_string $ b $ from flask import stream_with_context
$ b app = Flask(__ name__)
$ b $ app_route(/)
def server_1():
def generate_output() :
age = 0
template ='< p> {{name}}是{{age}}秒。< / p>'
context = {'name': 'bob'}
而真:
context ['age'] = age
yield render_template_string(template,** context)
time.sleep(5)
年龄+ = 5
返回响应(stream_with_context(generate_output()))
app.run()
下面是一些文档与Flask进行流式传输。
So there's this flask app that I'm working on for this project and I need it to run in a loop at timed variables to check for the status of certain variables and then give a output accordingly. However, the problem I have is I need to render a template in Flask before the loop restarts. In the changelog on http://flask.pocoo.org/ it's indicated that it's possible to render templates without using the request context but I haven't seen any real examples of this. So is there a way to render templates in Flask without having to use the request context without getting any errors? Any help that can be given is appreciated.
UPDATE: Here's the code I'm working with
from flask import Flask, render_template, request, flash, redirect, url_for
import flask
import time
from flask.ext.assets import Environment, Bundle
from flask_wtf import Form
from wtforms import TextField, TextAreaField, SubmitField
from wtforms.validators import InputRequired
CSRF_ENABLED = True
app = Flask(__name__)
app.secret_key = 'development key'
app = flask.Flask('my app')
assets = Environment(app)
assets.url = app.static_url_path
scss = Bundle('scss/app.scss', filters='scss', output='css/app.css')
assets.register('app_scss', scss)
@app.route('/')
def server_1():
r=1
g=2
b=3
i=g
if i == g:
with app.app_context():
print "Loading Template..."
rendered = flask.render_template('server_1.html', green=True)
print "Success! Template was loaded with green server status..."
time.sleep(5)
if __name__ == '__main__':
app.run(port=5000, debug=True)
You can do it by binding your application as the current application. Then you can use render_template()
to render a template from your template directory, or render_template_string()
to render directly from a template stored in a string:
import flask
app = flask.Flask('my app')
with app.app_context():
context = {'name': 'bob', 'age': 22}
rendered = flask.render_template('index.html', **context)
with app.app_context():
template = '{{ name }} is {{ age }} years old.'
context = {'name': 'bob', 'age': 22}
rendered = flask.render_template_string(template, **context)
Alternatively you could bypass Flask and go directly to Jinja2:
import jinja2
template = jinja2.Template('{{ name }} is {{ age }} years old.')
rendered = template.render(name='Ginger', age=10)
Update
It appears that you might be wanting to stream content back to the requesting client. If so you could write a generator. Something like this might work:
import time
from flask import Flask, Response, render_template_string
from flask import stream_with_context
app = Flask(__name__)
@app.route("/")
def server_1():
def generate_output():
age = 0
template = '<p>{{ name }} is {{ age }} seconds old.</p>'
context = {'name': 'bob'}
while True:
context['age'] = age
yield render_template_string(template, **context)
time.sleep(5)
age += 5
return Response(stream_with_context(generate_output()))
app.run()
Here is some documentation on streaming with Flask.
这篇关于如何在不使用请求上下文的情况下在烧瓶中渲染模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!