如何使html标记显示? [英] How to make html markup show up?

查看:64
本文介绍了如何使html标记显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是HTML的新手.我正在用烧瓶建立一个Web应用程序.我正在尝试可视化一些HTML标记,但显示不正确.

so I am new to HTML. I am building a web app with flask. I am trying to visualise some HTML markup, but it doen't show up properly.

我有以下烧瓶路线:

@app.route('/predict',methods=['GET','POST'])
def api():
    if request.method == "POST":

        input_data = request.form['rawtext']
        output_data = model_api(input_data)
        alignedText = output_data["words"]
        predictions = output_data["predictions"]
        #response = jsonify(output_data)
        print(predictions, flush=True)

        if request.values.get('type') == 'image':
            text = output_data["text"]
            ents = turnIntoSpacyFormat((predictions))
            inp = {"text": text, "ents": ents, "title": None}
            htmlm = displacy.render(inp, style="ent", manual=True)

            return render_template('index.html', text=alignedText, predictions=predictions, htmlm=htmlm)
        else:
            return render_template('index.html', text=alignedText, predictions=predictions)
    else:
        return render_template('index.html')

函数displacy.render返回html标记.我将其传递给index.html.我要打印的部分看起来如下(最后几行):

The function displacy.render returns the html markup. I am passing it to index.html. The part where I am trying to print it looks the following (last few lines):

      <!-- Result Display-->
  <section class="section section-solutions-about grey darken-2">
 <div class="container white-text">

      <!--   Icon Section   -->
      <div class="row">
        <div class="col s12 m6">
          <div class="icon-block">

            <!--<h5 class="center">Your Text</h5>-->
             <p>Text:&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0091EA;">{{ text }} </span></p>
            <!--<p class="light">{{ctext}}</p>-->
            <div class="alert alert-info" role="alert"><p>Entities: <span style="color:#0091EA;">{{ predictions }} </span></p><br/>
            </div>

          </div>
        </div>

      </div>
     <div class="entities" style="line-height: 2.5; direction: ltr"> Doesn't show up correctly</div>
     {{ htmlm }}

  </div>
</section>

我当时想我可以这样显示在html页面上:{{htmlm}}

I was thinking i could make it show up on the html page with that: {{ htmlm }}

显然,它打印标记的字符串表示形式.首先我以为标记可能是错误的.但是,当我在html文件中输入完全相同的内容时(如{{htmlm}}命令上方的一行),它会根据需要显示.

Apparently that prints a string representation of the markup though. First i thought that maybe the markup was faulty. But when I enter the exact same on in the html file like one line above the {{ htmlm }} command it shows up as desired.

这里是打印的内容:第一行是所需要的,并通过将html标记粘贴到html文件中来显示.第二个输出是我通过访问htmlm变量得到的结果.

Here is what is printed: The first line is what is desired and shows up by pasting the html markup into the html file. The second output is what I am getting through accessing the htmlm variable.

有人知道该怎么做吗?

推荐答案

生成的HTML被转义;您需要将其标记为 {{htmlm | safe}} .

The generated HTML is being escaped; you need to mark it as {{ htmlm|safe }}.

根据 Jinja文档:

...默认配置为不自动转义.对于各种原因:

... The default configuration is no automatic escaping; for various reasons:

  • 转义除安全值以外的所有内容还意味着Jinja正在转义已知不包含HTML的变量(例如数字,布尔值),这可能会对性能产生巨大影响.
  • 有关变量安全性的信息非常脆弱.通过强迫安全和不安全的值,返回值可能会发生是两次转义的HTML.

但是在Flask中,在许多情况下,自动转义设置为默认值:

However, in Flask, auto-escaping is set as the default in many cases:

除非进行自定义,否则Flask会对Jinja2进行如下配置:

Unless customized, Jinja2 is configured by Flask as follows:

  • 在使用render_template()时,所有以.html,.htm,.xml和.xhtml结尾的模板均启用了自动转义.
  • 使用render_template_string()时,会为所有字符串启用
  • 自动转义.模板可以选择加入/退出使用{%autoescape%}标签自动转义.
  • Flask在Jinja2上下文中插入了几个全局函数和辅助函数,此外还提供了默认.
  • autoescaping is enabled for all templates ending in .html, .htm, .xml as well as .xhtml when using render_template().
  • autoescaping is enabled for all strings when using render_template_string(). a template has the ability to opt in/out autoescaping with the {% autoescape %} tag.
  • Flask inserts a couple of global functions and helpers into the Jinja2 context, additionally to the values that are present by default.

这篇关于如何使html标记显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆