• <small id='WWBdM'></small><noframes id='WWBdM'>

    <legend id='WWBdM'><style id='WWBdM'><dir id='WWBdM'><q id='WWBdM'></q></dir></style></legend>

      1. <tfoot id='WWBdM'></tfoot>
        <i id='WWBdM'><tr id='WWBdM'><dt id='WWBdM'><q id='WWBdM'><span id='WWBdM'><b id='WWBdM'><form id='WWBdM'><ins id='WWBdM'></ins><ul id='WWBdM'></ul><sub id='WWBdM'></sub></form><legend id='WWBdM'></legend><bdo id='WWBdM'><pre id='WWBdM'><center id='WWBdM'></center></pre></bdo></b><th id='WWBdM'></th></span></q></dt></tr></i><div id='WWBdM'><tfoot id='WWBdM'></tfoot><dl id='WWBdM'><fieldset id='WWBdM'></fieldset></dl></div>

        • <bdo id='WWBdM'></bdo><ul id='WWBdM'></ul>

        如何在不使用请求上下文的情况下在烧瓶中呈现模板

        时间:2023-09-02
        • <tfoot id='ijEnW'></tfoot>
          <i id='ijEnW'><tr id='ijEnW'><dt id='ijEnW'><q id='ijEnW'><span id='ijEnW'><b id='ijEnW'><form id='ijEnW'><ins id='ijEnW'></ins><ul id='ijEnW'></ul><sub id='ijEnW'></sub></form><legend id='ijEnW'></legend><bdo id='ijEnW'><pre id='ijEnW'><center id='ijEnW'></center></pre></bdo></b><th id='ijEnW'></th></span></q></dt></tr></i><div id='ijEnW'><tfoot id='ijEnW'></tfoot><dl id='ijEnW'><fieldset id='ijEnW'></fieldset></dl></div>

            <legend id='ijEnW'><style id='ijEnW'><dir id='ijEnW'><q id='ijEnW'></q></dir></style></legend>

              <bdo id='ijEnW'></bdo><ul id='ijEnW'></ul>

                  <tbody id='ijEnW'></tbody>
              1. <small id='ijEnW'></small><noframes id='ijEnW'>

                • 本文介绍了如何在不使用请求上下文的情况下在烧瓶中呈现模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我正在为这个项目开发这个烧瓶应用程序,我需要它在定时变量处循环运行,以检查某些变量的状态,然后相应地给出输出.但是,我遇到的问题是我需要在循环重新启动之前在 Flask 中渲染一个模板.在 http://flask.pocoo.org/ 的更新日志中,它表明可以在不使用的情况下渲染模板请求上下文,但我还没有看到任何真实的例子.那么有没有一种方法可以在 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)
                  

                  推荐答案

                  您可以通过将您的应用程序绑定为当前应用程序来实现.然后你可以使用 render_template() 从你的模板目录渲染一个模板,或者 render_template_string() 直接从一个存储在字符串中的模板渲染:

                  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)
                  

                  您也可以绕过 Flask 直接进入 Jinja2:

                  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)
                  

                  更新

                  您可能希望将内容流式传输回发出请求的客户端.如果是这样,您可以编写一个生成器.这样的事情可能会奏效:

                  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.

                  这篇关于如何在不使用请求上下文的情况下在烧瓶中呈现模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在函数中使用局部变量并返回它? 下一篇:Swig,返回一个双精度数组

                  相关文章

                • <tfoot id='n1JhA'></tfoot>
                    • <bdo id='n1JhA'></bdo><ul id='n1JhA'></ul>

                    <i id='n1JhA'><tr id='n1JhA'><dt id='n1JhA'><q id='n1JhA'><span id='n1JhA'><b id='n1JhA'><form id='n1JhA'><ins id='n1JhA'></ins><ul id='n1JhA'></ul><sub id='n1JhA'></sub></form><legend id='n1JhA'></legend><bdo id='n1JhA'><pre id='n1JhA'><center id='n1JhA'></center></pre></bdo></b><th id='n1JhA'></th></span></q></dt></tr></i><div id='n1JhA'><tfoot id='n1JhA'></tfoot><dl id='n1JhA'><fieldset id='n1JhA'></fieldset></dl></div>

                      <small id='n1JhA'></small><noframes id='n1JhA'>

                      <legend id='n1JhA'><style id='n1JhA'><dir id='n1JhA'><q id='n1JhA'></q></dir></style></legend>